loudubewe Posted April 29, 2018 Share Posted April 29, 2018 I wrote this very short php page to test the javascript function submit(). <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head></head><body> <form id="myform" action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <input id="myinput" name="myinputname" type="hidden"> </form> <?php echo "<pre>Content of \$_POST\n"; print_r($_POST); echo "</pre>"; echo "<a href=\"\" onclick=\"examine('Hello')\">examine</a>"; ?> <script type="text/javascript" > function examine(parametre) { document.getElementById('myinput').value = parametre; document.getElementById('myform').submit(); } </script> </body></html> When the "examine" link is clicked, the submit function is executed, but after the "action" has reloaded the page, the $_POST array is empty ! If I put a breakpoint on the submit function (using the debugger of my firefox browser) and then step over that function, the $_POST array is OK. I try to understand why this behavior occurs. Is it a timing problem ? The same with the other php page here attached. Thanks for any advice. submit-test2.php Link to comment Share on other sites More sharing options...
dsonesuk Posted April 29, 2018 Share Posted April 29, 2018 Because the link on clicking, does want it normally does (even though it is empty, it is still linking to itself) so the update of hidden input then submission does get a chance to execute. You need to prevent the anchor doing what it normally does with event.prevent.Default() php echo "<a href=\"\" onclick=\"examine('Hello',event)\">examine</a>"; JS function examine(parametre, e) { e.preventDefault(); document.getElementById('myinput').value = parametre; document.getElementById('myform').submit(); } 1 Link to comment Share on other sites More sharing options...
loudubewe Posted May 12, 2018 Author Share Posted May 12, 2018 Thanks a lot for that clear explanation and ... solution. 😘 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now