Page 1 of 1

Minimum 2 txt fields required in a form to hit Enter Key

Posted: Sun Jan 18, 2004 6:06 am
by anjanesh
This is something that has taken me quite a while to figure this out.

Code 1 :

Code: Select all

<FORM ACTION="Query.php">
Name :
<INPUT name=txtName>
<INPUT type="Submit" value="Search" name="Search">
</FORM>

Code 2:

Code: Select all

<FORM ACTION="Query.php">
Name :
<INPUT name=txtName>
<INPUT name=dummy>
<INPUT type="Submit" value="Search" name="Search">
</FORM>

Code 1 works only if you click the Submit button
Code 2 works if you click the Submit button or hit the enter key in any one of the text fields (txtName or dummy).

So a minimum of 2 text input fileds are required to get it work on hitting the enter key in the fields.
If there is just 1 field we have to click the Submit button.

On hitting the enter key in the txtName field :
Code 1 returns : http://localhost/Query.php?txtName=something
Code 2 returns : http://localhost/Query.php?txtName=some ... rch=Search

In my PHP code I have :

Code: Select all

if (isset(Search))
So how do I resolve this ? I need the code to work even if someone hits the enter key in a 1 text field only form.

Thanks

Posted: Sun Jan 18, 2004 7:07 am
by redmonkey
You could just use....

Code: Select all

if(isset($_GET['txtName']))
I never rely on the 'Submit' button being clicked, as many browsers don't pass the value of the form submit unless explicitly clicked.

Posted: Sun Jan 18, 2004 7:08 am
by vigge89
I think you can make it work with one field in javascript, just search for some javascript sites and browse their 'forms'-section