Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    2 fields 2 submit buttons and pressing the enter key

    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.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    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.
    Last edited by adios; 10-15-2002 at 06:40 PM.

  • #3
    New Coder
    Join Date
    Jun 2002
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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"

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    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:

    Code:
    <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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •