Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Submitting a form by hitting Return

Status
Not open for further replies.

Halloko

Programmer
Apr 8, 2005
11
DK
Hey all,

I'm having some problems with ordinary HTML forms.
Have a look at the example here:
Say I've named my submit button "DoSubmit" and given it the value "Submit". Now, if I hit the submit button by using my mouse, the form is submitted and the value of "DoSubmit" is correctly sent to the server (as can be seen if you try clicking the mouse in my example page, above).
However, if you use the return key instead of the mouse (and hence rely on the default button) the form is still submitted but the value of "DoSubmit" isn't sent.

Below is the (interesting) code for the page:
Code:
<?php
	echo 'DoSubmit: ' . $_POST["DoSubmit"] .'<br/>';
	echo 'Cancel: ' . $_POST["Cancel"] . '<br/>';
?>

<form action='problem3.php' method='post'>
	<input type='text' name='foo' id='foo'/><br/>
	<input type='submit' name='DoSubmit' id='DoSubmit' value='Submit'/>&nbsp;
	<input type='button' name='Cancel' id='Cancel' value='Annuller'/>
</form>
Does anybody know why this is happening and how I can avoid this behavior?

Thanks!
 
yes, because you're not clicking on it. Make a hidden field:

Code:
<input type="hidden" name="DoSubmit" value="Submit" />

It will get submitted regardless of how you submit the form.

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
That was fast :eek:

Anyway, I just thought that all controls specified inside the <form> tags were automatically sent.. regardless of whether you clicked or used your keyboard.

But thanks for the heads up. You rock, people :p
 
You might also want to be careful with checkboxes - it has been a while but as I recall they are not sent as checked or unchecked but rather they are only sent if they are checked.

Just for future reference.

Crystal
--------------------------------------------------

Experience is one thing you can't get for nothing.

-Oscar Wilde

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top