0

I have been trying for a while now trying to figure out how to programmatically click a link using PHP and/or javascript. I have it setup so if the user clicks a link it will refresh a table. You don't really need to know why I want to do this b/c then it will go down a whole long road of confusion. Just know that there is a link to be clicked and I really really want to programmatically click that link using PHP and/or javascript.

Is there really no way to do this?

Edit: The code where I need to put the auto-click is in PHP, which would have to create and trigger some javascript or jquery or whatever.

Edit 2: Ok, now that you're all confused ... the real problem is that I have a Drupal form that has a property set to use AJAX when submitting. So the submission is done using the jquery plugin that is a module for Drupal. The AJAX setting is just an attribute setting and I do not have access to the underlying code that goes along with the submission of the form. Which forces me to have to refresh the table after the button is clicked. I really wish I could just attach the refreshing to the button click event for the submit of the form. But since I don't have access to that code I don't believe it's possible.

9
  • 1
    With regard to you specifically saying that we don't need to know why: Why do you need to click the link rather than just setting off the event that happens when you click the link? Because if it is a Javascript function you're looking to set off, why not just call the function? If you're looking to go some place, why not just tell the browser to navigate there with Javascript? Commented Feb 9, 2010 at 5:33
  • 1
    If you can't explain your intentions without confusing us, perhaps you're the one who is confused? Commented Feb 9, 2010 at 5:34
  • Nope, I"m not confused ... there are constraints that take too long to explain. People will suggest that I change the way I'm going about it, but I CAN'T!! Commented Feb 9, 2010 at 5:37
  • You should change the way that you are going about it, completely Commented Feb 9, 2010 at 5:40
  • See! haha. But no seriously, the refreshing works in an intricate way that requires clicking a link. Commented Feb 9, 2010 at 5:43

2 Answers 2

4

With Javascript, you can since it runs on the client machine, where the link exists. But the link doesn't even exist when PHP is doing it's magic, so you cannot click it "with" PHP. Keep in mind that PHP runs on the server, but the link exists only on the client.

Click a link with Javascript is rather simple:

// <a href="index.php" id="mylink">Index Page</a>
document.getElementById("mylink").click();

Make sure all of your values are spelled properly. You can even output this command from PHP:

    <?php print "<script type='text/javascript'>
                   document.getElementById('myLink').click();
                 </script>"; ?>
  </body>
</html>

Note I placed this just before the closing </body> tag to ensure the link is present on the page.

Sign up to request clarification or add additional context in comments.

2 Comments

You 'might' want to put that inside script tags.... or at least hint that it is inside script tags by putting the closing </script> Edit:nvm....
Even thought this didn't really get me to a solution, it helped me realize it really isn't possible b/c the link is on the client and PHP is on the server. I will have to look into a better way to do this whole thing. Thanks!
0

Since it is drupal i assume that the form you're speaking of has an URL and therefore you could inject javascript code with the following module: JS Injector

3 Comments

I don't think I can. The form doesn't have a URL ... it is a form embedded within a block provided in the code of a module I created. I use the forms api for Drupal to code the form. If you can provide an example of how I might do this in code that would be awesome.
If it is within a block, i would suggest to use drupal_add_js for injecting javascript code into your block. In combination with jquery's document-ready-trigger you could fire a click event, when the page (and therefore also the block) is fully loaded.
I have and am using drupal_add_js already in that drupal module that create the block in question. However, my needs are really to be able to call some javascript in the mymodule_form_submit() PHP function provided by Drupal forms. When I submit the form it gets in that code and makes the call to the database. Once the database call is successful then I want to refresh the table. But maybe there is an easier way to hook into the Drupal form itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.