View Full Version : 2 fields 2 submit buttons and pressing the enter key

10-15-2002, 04:04 PM
I have one form on my site with two submit buttons. A user chooses which submit button to press depending on which text box they filled out. My problem comes in that if they press the enter key it will automatically use the first submit button even if they are typing in the field which needs the second submit button pressed. I am using PHP to process the input, is there any way to force the correct submit button to press if enter is pressed in its appropriate text box.

10-15-2002, 04:35 PM
Could you explain this in just a bit more detail: the methodology behind it, how the text boxes are chosen (easy to make them mutually exclusive) and so on. Couldn't hurt.

10-15-2002, 06:19 PM
Well the user uses the text box to search a database. If they enter in a value in the top field it shows one set of results and if they enter in a value in the second field it shows a different result. For various reasons, which are too detailed to go into, it has to be set up like this

so you have something like:

|Field one|
|Search Field 1|

|Field two|
|Search Field 2|

my users are putting a value in Field 2 but when they press the Enter key it submits as if the "Search Field 1" button was pressed.

In order to determine what to search on my PHP checks to see which submit button was pressed and acts accordingly. I would like to know how to force it to submit as if "Search Field 2" button was pressed when the enter key is pressed while in "Field two"

10-15-2002, 07:50 PM
You could keep the form from submitting if they press the enter key. Something like this in the head section:
function KeyDownc(e)
var keycodec = event.keyCode
return false;

This is set up for IE only.

10-16-2002, 03:55 AM
Let me know if this works. ;) Put a flag (whichsearch) in a hidden field, hope you can incorporate it into your PHP. Works in Navigator (enter = submit) as well:

<title>untitled</title><script type="text/javascript" language="javascript">

function checkEnter(e) {
var kC = window.event ? event.keyCode :
e && e.keyCode ? e.keyCode :
e && e.which ? e.which : null;
if (kC) return kC == 13;

function checkEmpty(field) {
if (!field.value) {
alert('Please enter search terms.');
return false;
return true;

<input type="text" name="searchfield1" onkeypress="if(checkEnter(event))search1.click()">
<input type="button" name="search1" value="Search"
<input type="text" name="searchfield2" onkeypress="if(checkEnter(event))search2.click()">
<input type="button" name="search2" value="Search"
<input type="hidden" name="whichsearch" value="">