Enter as default instead of button click in <FORM>

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

Enter as default instead of button click in <FORM>

Post by anjanesh »

I have a html file :

Code: Select all

&lt;HTML&gt;
&lt;HEAD&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;FORM ACTION="Query.php"&gt;
Name :
&lt;INPUT name=txtName&gt;
&lt;INPUT type="Submit" value="Search" name=SubmitBasicSearch&gt;
&lt;/FORM&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
Enter some name say John.

If we hit the enter key, it goes here :
http://localhost/Query.php?txtName=john

If we click then search button, it goes here :
http://localhost/Query.php?txtName=john ... rch=Search

In my php code of Query.php I have this

Code: Select all

<?php
if (isset($SubmitBasicSearch)) SearchName($txtName);
?>
Obviously this won't work if we hit the enter key. Only if the button is clicked then it'll work. So how do I resolve this ?
I already tried a javascript code for onkeypress in the txtName(gave id for that) and did document.open() etc etc but it didn't work either.
I do not want to do

Code: Select all

<?php
if (isset($txtName)) SearchName($txtName);
?>
because the actual page has multiple forms with same action link.

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you might add an input/hidden that carries the data

Code: Select all

<INPUT type="hidden" value="Search" name="SubmitBasicSearch" />
<INPUT name="txtName" />
<INPUT type="Submit" value="Search" />
Post Reply