Jump to content

submit() - timing problem


Recommended Posts

I wrote this very short php page to test the javascript function submit().

<form id="myform" action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input id="myinput" name="myinputname" type="hidden">
	echo "<pre>Content of \$_POST\n";
	echo "</pre>";
	echo "<a href=\"\" onclick=\"examine('Hello')\">examine</a>";
<script type="text/javascript" >
	function examine(parametre) {
			document.getElementById('myinput').value = parametre;

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.


Link to comment
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()


echo "<a href=\"\" onclick=\"examine('Hello',event)\">examine</a>";


function examine(parametre, e) {
                document.getElementById('myinput').value = parametre;


  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

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

  • Create New...