Jump to content
loudubewe

submit() - timing problem

Recommended Posts

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

Share this post


Link to post
Share on other sites

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();
            }

 

  • Like 1

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×