JavaScript - two submit buttons

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • houstoncity2004@yahoo.com

    #1

    JavaScript - two submit buttons

    Hi, I need help to JavaScript. I am new to this, and am trying to the
    page online ASAP. I have two submit buttons on one form, and each
    button will have to do different things.

    I named both button "but1". The first button has the value "Continue",
    the second button has the value "new page". If someone click on the
    button with the value "Continue", I need to check if certain
    information has been filled in before proceeding, and if yes, I need to
    go to page1.php (which is the second page of the form) and I have to
    save the result on a database.

    If someone click on the button with the value "New Page", I do not need
    to check for anything, but I have to go to pagenew.php (which is a
    different version of the second page) and also save the result onto a
    database.

    I have the following codes, I have been trying for the whole day, it is
    still not working. Would some please help me? Thank you very, very
    much!

    <script language="JavaS cript" type="text/javascript">
    <!--

    function radio_button_ch ecker()
    {

    if(radio_form.b ut1.value == "Continue")
    {
    if((radio_form. Desc.value == "")
    || (radio_form.Gen eral.value == 0)
    || (radio_form.Spe cific.value == 0))
    {
    alert("Please answer all questions.")
    return (false);
    }
    return (true);
    }

    }

    function decideLink()
    {
    if(radio.form.b ut1.value == "Continue")
    {
    return (page1.php);
    }
    if (radio.form.but 1.value == "Would Not Outsource Offshore")
    {
    return (pagenew.php);
    }
    }
    -->
    </script>


    <form method="post" action="return decideLink()"
    onsubmit="retur n radio_button_ch ecker()" name="radio_for m">

  • Ivan Marsh

    #2
    Re: JavaScript - two submit buttons

    On Fri, 11 Feb 2005 08:18:05 -0800, houstoncity2004 wrote:
    [color=blue]
    > Hi, I need help to JavaScript. I am new to this, and am trying to the
    > page online ASAP. I have two submit buttons on one form, and each
    > button will have to do different things.
    >
    > I named both button "but1". The first button has the value "Continue",
    > the second button has the value "new page". If someone click on the
    > button with the value "Continue", I need to check if certain information
    > has been filled in before proceeding, and if yes, I need to go to
    > page1.php (which is the second page of the form) and I have to save the
    > result on a database.
    >
    > If someone click on the button with the value "New Page", I do not need
    > to check for anything, but I have to go to pagenew.php (which is a
    > different version of the second page) and also save the result onto a
    > database.[/color]

    Correct me if I'm wrong folks but as I understand it either submit button
    will always pass the value of the last submit button on the form.

    I'd use regular buttons instead of submit buttons, each one triggering a
    different onclick function that submits the form.

    --
    Life is short, but wide. -KV

    Comment

    • RobB

      #3
      Re: JavaScript - two submit buttons

      houstoncity2004 @yahoo.com wrote:[color=blue]
      > Hi, I need help to JavaScript. I am new to this, and am trying to[/color]
      the[color=blue]
      > page online ASAP. I have two submit buttons on one form, and each
      > button will have to do different things.
      >
      > I named both button "but1". The first button has the value[/color]
      "Continue",[color=blue]
      > the second button has the value "new page". If someone click on the
      > button with the value "Continue", I need to check if certain
      > information has been filled in before proceeding, and if yes, I need[/color]
      to[color=blue]
      > go to page1.php (which is the second page of the form) and I have to
      > save the result on a database.
      >
      > If someone click on the button with the value "New Page", I do not[/color]
      need[color=blue]
      > to check for anything, but I have to go to pagenew.php (which is a
      > different version of the second page) and also save the result onto a
      > database.
      >
      > I have the following codes, I have been trying for the whole day, it[/color]
      is[color=blue]
      > still not working. Would some please help me? Thank you very, very
      > much!
      >
      > <script language="JavaS cript" type="text/javascript">[/color]

      'language' attribute deprecated (adios).
      [color=blue]
      > <!--[/color]

      Stop hiding ! (ancient history)
      [color=blue]
      > function radio_button_ch ecker()
      > {
      >
      > if(radio_form.b ut1.value == "Continue")[/color]

      You've got two form elements named 'but1', so the first is now
      referenced as but1[0]
      and the other is but1[1]. Their values are exactly as you wrote them in

      your HTML, no need to check. ;D

      Need to make this *document*.radi o_form (or use
      document.getEle mentById('radio _form') with an id.
      [color=blue]
      > {
      > if((radio_form. Desc.value == "")
      > || (radio_form.Gen eral.value == 0)[/color]

      Form values are strings ---> document.radio_ form.Specific.v alue == '0'

      Are those zeros text entries? Hard to tell without the form.
      [color=blue]
      > || (radio_form.Spe cific.value == 0))
      > {
      > alert("Please answer all questions.")
      > return (false);
      > }
      > return (true);
      > }
      >
      > }
      >
      > function decideLink()
      > {
      > if(radio.form.b ut1.value == "Continue")
      > {
      > return (page1.php);
      > }
      > if (radio.form.but 1.value == "Would Not Outsource Offshore")
      > {
      > return (pagenew.php);
      > }
      > }[/color]

      Bad news all around (see below)
      [color=blue]
      > -->[/color]

      Stop hiding !
      [color=blue]
      > </script>
      >
      >
      > <form method="post" action="return decideLink()"[/color]

      A form's action attribute takes a string url (or an empty string if the
      form is submitting to itself). This is not a (JavaScript) event
      handler. You can use a javascript: url to call JS
      (action="javasc ript:doThis()") but that's rarely useful.
      [color=blue]
      > onsubmit="retur n radio_button_ch ecker()" name="radio_for m">[/color]

      Try this:

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">
      <html>
      <head>
      <title>untitled </title>
      <script type="text/javascript">

      function Continue()
      {
      var f, els;
      if (f = document.getEle mentById('radio _form'))
      {
      els = f.elements;
      if((els.Desc.va lue == "")
      || (els.General.va lue == '0')
      || (els.Specific.v alue == '0'))
      {
      alert("Please answer all questions.")
      return false;
      }
      else f.action = 'page1.php';
      }
      }

      function Newpage()
      {
      var f;
      if (f = document.getEle mentById('radio _form'))
      f.action = 'pagenew.php';
      }

      </script>
      </head>
      <body>
      <form id="radio_form " name="radio_for m" action="page1.p hp"
      method="post">
      <input type="text" name="Desc" value="" />
      <br />
      <input type="text" name="General" value="0" />
      <br />
      <input type="text" name="Specific" value="0" />
      <br />
      <input type="submit" name="sub" value="Continue "
      onclick="this.f orm.onsubmit=Co ntinue" />
      <input type="submit" name="sub" value="New Page"
      onclick="this.f orm.onsubmit=Ne wpage" />
      </form>
      </body>
      </html>

      Clicking either button assigns a new onsubmit handler, defined above.
      If JS is disabled, the form will submit anyway, to the default action.
      Not sure enough about what is going on here to say more.

      Comment

      • RobG

        #4
        Re: JavaScript - two submit buttons

        RobB wrote:[color=blue]
        >
        > Form values are strings ---> document.radio_ form.Specific.v alue == '0'
        >[/color]

        Strictly speaking, yes. But in this case testing ...value == 0
        or ...value == '0' will return true (Firefox & IE).

        I guess it's an artifact of JavaScript's type conversion.

        Try this:

        <form action="">
        <input type="text" name="tst" value="0">
        <input type="button" value="check" onclick="
        var msg = '0 test: ';
        msg += (this.form.tst. value == 0);
        msg += '\n\'0\' test: ';
        msg += (this.form.tst. value == '0');
        msg += '\nisNaN test: ';
        msg += (isNaN(this.for m.tst.value));
        alert(msg);
        ">
        </form>


        --
        Rob

        Comment

        • RobB

          #5
          Re: JavaScript - two submit buttons

          RobG wrote:[color=blue]
          > RobB wrote:[color=green]
          > >
          > > Form values are strings ---> document.radio_ form.Specific.v alue ==[/color][/color]
          '0'[color=blue][color=green]
          > >[/color]
          >
          > Strictly speaking, yes. But in this case testing ...value == 0
          > or ...value == '0' will return true (Firefox & IE).
          >
          > I guess it's an artifact of JavaScript's type conversion.[/color]

          'Strictly speaking'...as in..."that's correct"?

          I think there is arguably a way to be overly elaborate for posters just
          learning the basics (not being condescending there)...someti mes I
          prefer to be straightforward about basic issues. Form elements are
          strings, and type conversion shouldn't, I think, be relied upon to
          'fix' things.
          [color=blue]
          >
          > Try this:
          >
          > <form action="">
          > <input type="text" name="tst" value="0">
          > <input type="button" value="check" onclick="
          > var msg = '0 test: ';
          > msg += (this.form.tst. value == 0);
          > msg += '\n\'0\' test: ';
          > msg += (this.form.tst. value == '0');
          > msg += '\nisNaN test: ';
          > msg += (isNaN(this.for m.tst.value));
          > alert(msg);
          > ">
          > </form>[/color]

          Very messy HTML - cute formatting, but prone to errors. (jmo)

          Comment

          Working...