SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2007
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Which submit button was clicked?

    I have a form with 2 submit buttons

    I have the following:

    Code HTML4Strict:
    <form action="page.php" method="post" onsubmit="return errorsOnSubmit(this);">

    My question is how can I in my errorsOnSubmit() function check which submit button was clicked??


    Cheers

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are different ways of doing it. Here's one example:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Template</title>
    <script type='text/javascript'>
    
    window.onload = function() {
      var e = document.getElementById('form1');
      if (e) {
        e.onsubmit = errorsOnSubmit;
        e = document.getElementById('btn1');
        e.onclick = errorsOnSubmit;
        e = document.getElementById('btn2');
        e.onclick = errorsOnSubmit;
      }
    };
    
    function errorsOnSubmit() {
      if (this.id == 'btn1') {
        alert("submit via pressing btn1");
        this.form.submit();
      }
      else if (this.id == 'btn2') {
        alert("submit via pressing btn2");
        this.form.submit();
      }
      else {
        alert("submit via pressing the 'enter' key");
        return true; // return false to cancel submission
      }
    }
    
    </script>
    </head>
    <body>
    
    <form action="page.php" method="post" id='form1'>
      <p><input type='text' value = 'press Enter to submit'></p>
      <p><input type='button' value='Submit 1' id='btn1'></p>
      <p><input type='button' value='Submit 2' id='btn2'></p>
    </form>
    
    </body>
    </html>


Bookmarks

Posting Permissions

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