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 Wanet Telecoms Ltd 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 pushing <enter>

Status
Not open for further replies.

newphpbie

Programmer
Oct 17, 2003
110
GB
I have a user search form. THe user can search records with one criteria. When they have entered the searching criteria they must click on the submit button to perform the search.

As it is now, if the user enters the serach criteria and pushes <enter> the search doesn't go through... it will only work if they click on the Submit button.

What I would like to know is if there is a way to link the submit button with the input box so when they have entered the criteria they can just push <enter> and this will 'click' the submit button.

I know about tabindex which is very very smiilar to what I want to do, but it doesn't do the job.

Does anyone understand what I'm trying to acheive with this, and can you help??

This is a bit of my code....

I've tried mvoing the last</input> tag to after the submit button, but still no joy.

Code:
echo '<form method=&quot;POST&quot; action=&quot;'.$PHP_SELF.'&quot;>';
echo '<select name=&quot;searchby&quot;>';
echo '<option value=&quot;Outlet&quot;>Outlet</option>';
echo '<option value=&quot;Cardport&quot;>Card Port</option>';
echo '<option value=&quot;Extnum&quot;>Ext Num</option>';
echo '</select><br><br>';
echo 'Search Field: <br><input type=&quot;Text&quot; name=&quot;searchitem&quot;></input><br><br><br>';
echo '<input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;SEARCH&quot;></form><br><br>';

Any idea's from anyone?
 
How do you check for the fact that the form was submitted?

If you just use the submit button var you'll encounter this phenomeon. Some older browsers also handle submit buttons differently.

Suggestion:
Put a hidden field into the form called &quot;posted&quot; with a dummy value. Test for the existence of that field in $_POST and trigger the code execution by that.

Code:
<!-- HTML -->
<input type=&quot;hidden&quot; name=&quot;posted&quot; value=&quot;yes&quot;>
# PHP code
<?php
if ($_POST['posted']){
  ...etc.
 
It should work automatically anyway, try removing the </input> tag all together.

This is the most simplest type of form you can make, and it works


<form action=&quot; method=&quot;post&quot;>
<input name=&quot;&quot; type=&quot;text&quot;><br>
<input name=&quot;Submit&quot; type=&quot;submit&quot;>
</form>

Just hitting enter in the edit box will send you off to microsoft.com, or you can click the submit button.

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
As Skute said, remove the </INPUT> tags. They shouldn't be there since <INPUT> is not pair tag. After removing the tags, your form should be submited using <ENTER> key.
 
DRJ478 : Thanks for that, I've tried it and it worked. I can't work out why the 'if' statement will work for the hidden value by just pushing enter but not for the actual button value.

The code I had following my input form was...

Code:
if($_POST['submit']==&quot;Log In&quot;){
 blah blah blah
but now I've changed it to
Code:
if($_POST['posted']{
//if($_POST['submit']==&quot;Log In&quot;){
 blah blah blah
and it works....

SO why does 'if($_POST['posted']{' work when the <enter> key is pushed and not 'if($_POST['submit']==&quot;Log In&quot;){' ?

I've got it doing what I want it to now but I don't understand why it doesn't work the way I wanted it to.

Any idea's why?
 
It depends on the browser. NS4 for example always posts all buttons on the page - quite annoying. IE does not send the value of the submit button unless it is pressed.
However, when a form is submitted hidden fields are posted. Submission may be by a submit button, enter key (e.g. IE but not NS) or by JavaScript.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top