...

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



Darksbane
10-15-2002, 05: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.

adios
10-15-2002, 05: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.

Darksbane
10-15-2002, 07: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"

requestcode
10-15-2002, 08:50 PM
You could keep the form from submitting if they press the enter key. Something like this in the head section:
<SCRIPT LANGUAGE="JavaScript">
function KeyDownc(e)
{
var keycodec = event.keyCode
if(keycodec=='13')
{
return false;
}
}
document.onkeypress=KeyDownc;
</SCRIPT>

This is set up for IE only.

adios
10-16-2002, 04: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:



<html>
<head>
<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.');
field.focus();
return false;
}
return true;
}

</script>
</head>
<body>
<form>
<input type="text" name="searchfield1" onkeypress="if(checkEnter(event))search1.click()">
<input type="button" name="search1" value="Search"
onclick="whichsearch.value=this.name;if(checkEmpty(searchfield1))this.form.submit()"><br>
<input type="text" name="searchfield2" onkeypress="if(checkEnter(event))search2.click()">
<input type="button" name="search2" value="Search"
onclick="whichsearch.value=this.name;if(checkEmpty(searchfield2))this.form.submit()">
<input type="hidden" name="whichsearch" value="">
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum