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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts

    submit form with enter key in PHP

    I am trying to get my form which is in PHP to submit when pressing the Enter key

    This is what I am using

    Code:
    <script type="text/javascript">
    
    function submitFormWithEnter(myfield,e)
    {
       var keycode;
       if (window.event)
       {
          keycode = window.event.keyCode;
       }
       else if (e)
       {
          keycode = e.which;
       }
       else
       {
          return true;
       }
    
       if (keycode == 13)
       {
          myfield.form.submit();
          return false;
       }
       else
       {
          return true;
       }
    }
    </script>

    PHP Code:
    <div id='aboutForm'>
      <
    span class='headerbox'><b>Your Profile Headline:</b></span>
      <
    span class='textbox'><input type='text' name='headline' class='zip' size='67     ' value='$headline' 
          
    onKeyPress="return submitFormWithEnter(this,event)\"></span>
    </div> 
    Anyway I can get this to work?

    Seems to be an issue with the "text" input
    Last edited by harkly; 08-27-2010 at 08:48 PM.

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Posts
    271
    Thanks
    3
    Thanked 40 Times in 40 Posts
    Just add this to the code on the form. Warning though text areas will trigger it if they hit enter.

    <script language="javascript">
    document.onkeypress = function(){
    if(event.keyCode == 13)document.form_name.submit();
    }
    </script>

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This has nothing to do with PHP.
    Moving from PHP to Javascript.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    <script language="javascript">
    document.onkeypress = function(){
    if(event.keyCode == 13)document.form_name.submit();
    }
    </script>
    Ummmm...that code will ONLY work in MSIE. Only MSIE has a global event.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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