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

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

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

Post 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
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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
Post Reply