Which Submit button was pressed?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Syed Ali

    #1

    Which Submit button was pressed?

    Hello,

    I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.

    If submit button1 is pressed I need to make sure that all 10 textfield
    entries have been filled before submitting the form.

    If submit button2 is pressed I need to make sure that only textfied1
    is filled before submitting the form.

    How do I go about doing the above check?

    I used -onSubmit with the HTML form tab, but I am not sure how to
    check which button was pressed, i.e., how do I reference submit1,
    submit2 button in my logic of:
    if submit1 then
    if all textfields have been completed then
    return true
    else
    return false

    I also tried to used -onClick with each Submit button, but returning
    false if all textfields have not been filled still causes the form to
    get submitted to the CGI script because the HTML form submit itself
    has no -onSubmit handler.

    THank you!
  • Brian Kerrick Nickel

    #2
    Re: Which Submit button was pressed?

    > I used -onSubmit with the HTML form tab, but I am not sure how to[color=blue]
    > check which button was pressed, i.e., how do I reference submit1,
    > submit2 button in my logic of:
    > if submit1 then
    > if all textfields have been completed then
    > return true
    > else
    > return false
    >
    > I also tried to used -onClick with each Submit button, but returning
    > false if all textfields have not been filled still causes the form to
    > get submitted to the CGI script because the HTML form submit itself
    > has no -onSubmit handler.[/color]

    You could do something along the lines of:

    var canSubmit;

    ...

    <form onsubmit="retur n canSubmit;">

    <submit onclick="canSub mit = validator1( );" />

    Comment

    • Michael Hall

      #3
      Re: Which Submit button was pressed?

      try to write a function that will check to see
      if the forms are blank or not when the button is pressed.

      Can be done with one function or 2 functions. One woulds check all
      alternatively create 2 and assign one to each button.

      alii@paul.rutge rs.edu (Syed Ali) wrote in message news:<30bb3ae7. 0312160750.a698 348@posting.goo gle.com>...[color=blue]
      > Hello,
      >
      > I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.
      >
      > If submit button1 is pressed I need to make sure that all 10 textfield
      > entries have been filled before submitting the form.
      >
      > If submit button2 is pressed I need to make sure that only textfied1
      > is filled before submitting the form.
      >
      > How do I go about doing the above check?
      >
      > I used -onSubmit with the HTML form tab, but I am not sure how to
      > check which button was pressed, i.e., how do I reference submit1,
      > submit2 button in my logic of:
      > if submit1 then
      > if all textfields have been completed then
      > return true
      > else
      > return false
      >
      > I also tried to used -onClick with each Submit button, but returning
      > false if all textfields have not been filled still causes the form to
      > get submitted to the CGI script because the HTML form submit itself
      > has no -onSubmit handler.
      >
      > THank you![/color]

      Comment

      • Chris Riesbeck

        #4
        Re: Which Submit button was pressed?

        alii@paul.rutge rs.edu (Syed Ali) wrote in message news:<30bb3ae7. 0312160750.a698 348@posting.goo gle.com>...[color=blue]
        > Hello,
        >
        > I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.
        >
        > If submit button1 is pressed I need to make sure that all 10 textfield
        > entries have been filled before submitting the form.
        >
        > If submit button2 is pressed I need to make sure that only textfied1
        > is filled before submitting the form.
        >
        > How do I go about doing the above check?
        >
        > I used -onSubmit with the HTML form tab, but I am not sure how to
        > check which button was pressed, i.e., how do I reference submit1,
        > submit2 button in my logic of:
        > if submit1 then
        > if all textfields have been completed then
        > return true
        > else
        > return false
        >
        > I also tried to used -onClick with each Submit button, but returning
        > false if all textfields have not been filled still causes the form to
        > get submitted to the CGI script because the HTML form submit itself
        > has no -onSubmit handler.
        >[/color]

        Try using both methods. Have the submit button's onclick() methods
        set a global status variable to the result of their check of the
        text fields. Have the form's onsubmit() method check that variable.

        If you want to have an error message that varies depending
        on which button was clicked, have the onclick() methods store
        an error message if there's a problem. Otherwise they store
        null. The form's onsubmit() would display the error
        message, if any, and return false, otherwise return true.

        Comment

        • Lee

          #5
          Re: Which Submit button was pressed?

          Syed Ali said:[color=blue]
          >
          >Hello,
          >
          >I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.
          >
          >If submit button1 is pressed I need to make sure that all 10 textfield
          >entries have been filled before submitting the form.
          >
          >If submit button2 is pressed I need to make sure that only textfied1
          >is filled before submitting the form.
          >
          >How do I go about doing the above check?[/color]

          You can't check that for sure, because you don't know that the visitor
          has JavaScript enabled. If they have JavaScript enabled, you can check
          it for their convenience, but you also have to be able to validate on
          the server side, so you also need to provide a way for the server to
          know which button was pressed.

          This works on the client side, and also sends enough information to the
          server to allow it to perform the same sort of validation.

          Note that I'm taking your specification literally to mean that if the
          second button is pressed, that *only* the first field should be filled:


          <html>
          <head>
          <script type="text/javascript">
          function validate(f,whic hCase){
          var textfieldCount= 0;
          var hasValueCount=0 ;
          var firstHasValue=0 ;
          for(var i=0;i<f.element s.length;i++){
          if(f.elements[i].type=="text"){
          textfieldCount+ +;
          if(-1!=f.elements[i].value.search(/\S/)){
          hasValueCount++ ;
          if(textfieldCou nt==1){
          firstHasValue++ ;
          }
          }
          }
          }
          if(whichCase==" button1"){ // all must have values
          if(hasValueCoun t==textfieldCou nt){
          return true;
          }else{
          alert ("all fields must have values.");
          return false;
          }
          }else{ // only first may have value
          if((hasValueCou nt==1)&&(firstH asValue)){
          return true;
          }else{
          alert("the first field (only) must have a value");
          return false
          }
          }
          }
          </script>
          </head>
          <body>
          <form onsubmit="retur n validate(this,w hichPressed)">
          <input><input>< input><input>
          <input name="whichSubm it" type="submit" value="button1"
          onclick="whichP ressed=this.val ue">
          <input name="whichSubm it" type="submit" value="button2"
          onclick="whichP ressed=this.val ue">
          </form>
          </body>
          </html>

          Comment

          • Evertjan.

            #6
            Re: Which Submit button was pressed?

            Lee wrote on 16 dec 2003 in comp.lang.javas cript:
            [color=blue]
            > <input name="whichSubm it" type="submit" value="button1"
            > onclick="whichP ressed=this.val ue">
            > <input name="whichSubm it" type="submit" value="button2"
            > onclick="whichP ressed=this.val ue">
            >[/color]

            If you want the texts on the buttons to be the same, do:


            <input name="button1" type="submit" value="Submit me"
            onclick="whichP ressed=this.nam e">
            <input name="button2" type="submit" value="Submit me"
            onclick="whichP ressed=this.nam e">


            --
            Evertjan.
            The Netherlands.
            (Please change the x'es to dots in my emailaddress)

            Comment

            • @SM

              #7
              Re: Which Submit button was pressed?

              Syed Ali a ecrit :[color=blue]
              >
              > Hello,
              >
              > I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.
              >
              > If submit button1 is pressed I need to make sure that all 10 textfield
              > entries have been filled before submitting the form.
              >
              > If submit button2 is pressed I need to make sure that only textfied1
              > is filled before submitting the form.
              >
              > How do I go about doing the above check?[/color]

              <script type="text/javascript"><!--
              function verifOne(nameOf Field){
              if(nameOfField. value=='') {
              alert(nameOfFie ld.name+' not filled');
              nameOfField.foc us();
              return false;
              }
              else return true;
              }
              function verifAll(MyForm ){
              ok=0;
              for(var i=0;i<MyForm.le ngth;i++) {
              if(MyForm.eleme nts[i].type=="text")
              if(!(verif1(MyF orm.elements[i])))
              { ok=1; return; }
              }
              if(ok==0) return true;
              else return false;
              }
              // --></script>

              <form action="blabla" etc>
              <input type=button value="Submit one field"
              onclick="if(ver ifOne(this.form .Firstname)) this.form.submi t();">
              <input type=button value="Submit All"
              onclick="if(ver ifAll(this.form )) this.form.submi t();">
              </form>

              Not tried !

              -- -----
              @SM
              move away *OTEZ-MOI* from my reply url

              Comment

              • Syed Ali

                #8
                Re: Which Submit button was pressed?

                Thank you to everyone who responded.

                What worked me for me is setting a global var with -onlick on each
                submit button and then checking the value of that global var in
                -onSubmit for the form.

                For example, submit button1 -onclick sets global var
                whichButton=but ton1.
                And in -onSubmit function, I check the value of whichButton, if it is
                button1 then I know that all textfields need to be filled, if it is
                button2 then I make sure that only the 1 needed textfield is filled.

                Thanks again.

                alii@paul.rutge rs.edu (Syed Ali) wrote in message news:<30bb3ae7. 0312160750.a698 348@posting.goo gle.com>...[color=blue]
                > Hello,
                >
                > I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.
                >
                > If submit button1 is pressed I need to make sure that all 10 textfield
                > entries have been filled before submitting the form.
                >
                > If submit button2 is pressed I need to make sure that only textfied1
                > is filled before submitting the form.
                >
                > How do I go about doing the above check?
                >
                > I used -onSubmit with the HTML form tab, but I am not sure how to
                > check which button was pressed, i.e., how do I reference submit1,
                > submit2 button in my logic of:
                > if submit1 then
                > if all textfields have been completed then
                > return true
                > else
                > return false
                >
                > I also tried to used -onClick with each Submit button, but returning
                > false if all textfields have not been filled still causes the form to
                > get submitted to the CGI script because the HTML form submit itself
                > has no -onSubmit handler.
                >
                > THank you![/color]

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: Which Submit button was pressed?

                  Syed Ali wrote:
                  [color=blue]
                  > What worked me for me is setting a global var with -onlick on each
                  > submit button and then checking the value of that global var in
                  > -onSubmit for the form.
                  >
                  > For example, submit button1 -onclick sets global var
                  > whichButton=but ton1.
                  > And in -onSubmit function, I check the value of whichButton, if it is
                  > button1 then I know that all textfields need to be filled, if it is
                  > button2 then I make sure that only the 1 needed textfield is filled.[/color]

                  There is no need to spoil namespaces. Use `whichbutton' as a property
                  of a DOM object, e.g. the `HTMLFormElemen t' object itself. Or use the
                  `value' property of a input[type="hidden"] `HTMLInputEleme nt' object
                  instead.
                  [color=blue]
                  > [Top post][/color]

                  Please do not waste scarce resources.


                  PointedEars

                  Comment

                  Working...