[Javascript] Test or Identify Value of Submit Button's AssignedValue

Ron Wingfield rtwingfield at archaxis.net
Fri Jan 21 15:37:56 CST 2005


Thanks for the suggestions, Flavio.  Here's what I've learned.  Apparantly, the -onsubmit clause needs to be associated with the form to scope the form elements into the checkForm( ) function.

  # Client-side JavaScript Generated with the Perl CGI:

  print qq(<script><!--
      function checkForm ( form ) {
          var clicked=arguments[1];   // do this for "readability"; . . .points to 2nd arg!

          if ( clicked = = "Update")
          ||   clicked = = "Insert") {
              if ( form["vendor_name"].value = = "") {
                  alert("Vendor Name is required");
              return false;
              }
  //          other similar conditionals can be added per above.
          }
          return true;
      }
      // --></script>
  );
  ~
  ~ etc.
  ~
  $cgi->start_form(-action=>'vendors_01.pl',
           -method=>'GET',
           -name=>'vendors_01.pl',
           -onsubmit=>"return checkForm( this, clicked )"
      );

The submit buttons are defined as follows:
  $cgi-> submit(-name=>"action", -value=>"Inquire",   -onclick=>"clicked=value"),
  $cgi-> submit(-name=>"action", -value=>"Update",  -onclick=>"clicked=value"),
  $cgi-> submit(-name=>"action", -value=>"Insert",     -onclick=>"clicked=value"),
  $cgi-> submit(-name=>"action", -value=>"Delete",    -onclick=>"clicked=value"),

I'm not sure exactly how the value assigned to the var, "clicked" is passed to the checkForm functions, but is is passed as the second argument to the function.  It does work.

OTTF,
Ron Wingfield

FreeBSD 4.8  --  Apache http 2.0.28 -- MySQL client/server 4.1.7
Perl 5.8.5 -- p5-DBD-mysql-2.9004 driver -- p5-DBI-1.46


--------------------------------------------------------------------------------

  ----- Original Message ----- 
  From: Flavio Gomes 
  To: [JavaScript List] 
  Sent: Friday, January 21, 2005 12:02 PM
  Subject: Re: [Javascript] Test or Identify Value of Submit Button's AssignedValue


  Hello Ron,


  Maybe changing  your checkForm to something like

            function checkForm(pressedSubmit)   { ... }

  then call it as

            <input type=submit onclick="checkForm(this.value)" name=action 
  value=Inquire>
            <input type=submit onclick="checkForm(this.value)" name=action 
  value=Update>

  Or you could set a variable initiated with the default "value" of the 
  submit, and when the user clicks the button (the submit) it changes the 
  variable value to it's(button's) value.

  --
  Flavio Gomes
  flavio at economisa.com.br



  Ron Wingfield wrote:

  > I have defined some submit buttons, e.g., assoc/w "Inquire", "Update", 
  > etc.,  as follows:
  >  
  >
  >     $cgi-> submit(-name=>"action", -value=>"Inquire"),
  >     $cgi-> submit(-name=>"action", -value=>"Update"),
  >
  > Regarding the first "if" condition in the following JavaScript code 
  > (from the checkForm function executed -onsubmit), the value of  " 
  > form["action"].value " is /undefined/ (. . .at least at the point in 
  > my code); however, the value of " form[''name"].value " is the value 
  > as typed in the form's /Name/ field. 
  >
  >                  if ( form["action"].value == "Update") {
  >                       if ( form["name"].value == "") {
  >                           alert("Name is required");
  >                       return false;
  >                       }
  >                  }
  >
  > Obviously, the following -onsubmit condition is executing the 
  > JavaScript.  In the client-side JavaScript, how do I test the value of 
  > the clicked submit button?  In other words, I need to know which 
  > submit button was clicked.
  >
  >
  >     $cgi->start_form(-action=>'vendors_01.pl',
  >                   -method=>'GET',
  >                   -name=>'vendors_01.pl',
  >                   -onsubmit=>"return checkForm( this );" );
  >
  > Thanks,
  > Ron Wingfield
  >  
  > FreeBSD 4.8  --  Apache http 2.0.28 -- MySQL client/server 4.1.7
  > Perl 5.8.5 -- p5-DBD-mysql-2.9004 driver -- p5-DBI-1.46


  _______________________________________________
  Javascript mailing list
  Javascript at LaTech.edu
  https://lists.LaTech.edu/mailman/listinfo/javascript
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20050121/a6038398/attachment.htm>


More information about the Javascript mailing list