Two forms

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

    Two forms

    I have two forms on one page. In Form A I have drop-down list (single
    selection). When I click a submit button in form B, I would like to pick up
    the value showing in the drop down list of Form A. Can this be done?

    The reason for separating the two forms is that Form A opens a new browser
    window with selection in that form, whereas Form B keeps it in the same
    browser window for its selections. The drop-down list value, however, is
    used in both and I would not want to repeat that list in Form B.

    Shelly


  • NurAzije

    #2
    Re: Two forms


    Shelly wrote:
    I have two forms on one page. In Form A I have drop-down list (single
    selection). When I click a submit button in form B, I would like to pick up
    the value showing in the drop down list of Form A. Can this be done?
    >
    The reason for separating the two forms is that Form A opens a new browser
    window with selection in that form, whereas Form B keeps it in the same
    browser window for its selections. The drop-down list value, however, is
    used in both and I would not want to repeat that list in Form B.
    >
    Shelly
    You can do it by JavaScript or by AJAX, can you provide us with the
    code, so I can explain to you eaisier ??
    --------------------------------------------------------------------------------------

    For php/ajax/javascript tutorials and tips, visit me on my blog at


    Comment

    • Shelly

      #3
      Re: Two forms


      "NurAzije" <nurazije@gmail .comwrote in message
      news:1161003925 .272459.265460@ m73g2000cwd.goo glegroups.com.. .
      >
      Shelly wrote:
      >I have two forms on one page. In Form A I have drop-down list (single
      >selection). When I click a submit button in form B, I would like to pick
      >up
      >the value showing in the drop down list of Form A. Can this be done?
      >>
      >The reason for separating the two forms is that Form A opens a new
      >browser
      >window with selection in that form, whereas Form B keeps it in the same
      >browser window for its selections. The drop-down list value, however, is
      >used in both and I would not want to repeat that list in Form B.
      >>
      >Shelly
      >
      You can do it by JavaScript or by AJAX, can you provide us with the
      code, so I can explain to you eaisier ??
      I don't know AJAX.

      [ php code]
      <?php
      if (isset($_POST['A'])) {
      $list = $_POST['theList'];
      header('Locatio n: A_target.php?li st=' . $list);
      exit();
      }

      <?php
      if (isset($_POST['B'])) {
      $list = $_POST['theList'];
      header('Locatio n: B_target.php?li st=' . $list);
      exit();
      }
      ?>

      [html area]
      <form action="" method="POST" name="A" target="_blank" >
      <select name="theList">
      <option value="First'"> First </option>
      <option value="Second'" >Second </option>
      </select>
      ....other stuff...
      <input type="submit" value="Form A button" name="A">
      </form>

      <form action="" method="POST" name="B" target="_self">
      <input type="submit" value="Form A button" name="B">
      </form>


      Comment

      • Shelly

        #4
        Re: Two forms


        "Shelly" <sheldonlg.news @asap-consult.comwrot e in message
        news:RnSYg.7853 $Lv3.2073@newsr ead1.news.pas.e arthlink.net...
        >
        "NurAzije" <nurazije@gmail .comwrote in message
        news:1161003925 .272459.265460@ m73g2000cwd.goo glegroups.com.. .
        >>
        >Shelly wrote:
        >>I have two forms on one page. In Form A I have drop-down list (single
        >>selection). When I click a submit button in form B, I would like to
        >>pick up
        >>the value showing in the drop down list of Form A. Can this be done?
        >>>
        >>The reason for separating the two forms is that Form A opens a new
        >>browser
        >>window with selection in that form, whereas Form B keeps it in the same
        >>browser window for its selections. The drop-down list value, however,
        >>is
        >>used in both and I would not want to repeat that list in Form B.
        >>>
        >>Shelly
        >>
        >You can do it by JavaScript or by AJAX, can you provide us with the
        >code, so I can explain to you eaisier ??
        >
        I don't know AJAX.
        >
        [ php code]
        <?php
        if (isset($_POST['A'])) {
        $list = $_POST['theList'];
        header('Locatio n: A_target.php?li st=' . $list);
        exit();
        }
        >
        <?php
        if (isset($_POST['B'])) {
        $list = $_POST['theList'];
        header('Locatio n: B_target.php?li st=' . $list);
        exit();
        }
        ?>
        >
        [html area]
        <form action="" method="POST" name="A" target="_blank" >
        <select name="theList">
        <option value="First'"> First </option>
        <option value="Second'" >Second </option>
        </select>
        ....other stuff...
        <input type="submit" value="Form A button" name="A">
        </form>
        >
        <form action="" method="POST" name="B" target="_self">
        <input type="submit" value="Form A button" name="B">
        </form>

        That second one should be "Form B button" and the second <?php was a cut
        and paste error.


        Comment

        • NurAzije

          #5
          Re: Two forms

          For AJAX it is so easy, you have a 5 minutes tutorial on this link :


          You can put the second bottun in the first form, like this:
          <form action="" method="POST" name="Aform" target="_blank" >
          <select name="theList">
          <option value="First'"> First </option>
          <option value="Second'" >Second </option>
          </select>
          ....other stuff...
          <input type="submit" value="Form A button" name="A">
          <input type="button" value="Form B button" name="B"
          onclick="docume nt.Aform.submit ();">
          </form>
          You will see I have added onclick="docume nt.Aform.submit ();", and the
          type is button, put an action path for the form, and change the form
          name as I did, every element must have a unique name.
          The php code must look like this:
          <?php
          if (isset($_POST['A'])) {
          $list = $_POST['theList'];
          header('Locatio n: A_target.php?li st=' . $list);
          exit();
          }elseif(isset($ _POST['B'])) {
          $list = $_POST['theList'];
          header('Locatio n: B_target.php?li st=' . $list);
          exit();
          }
          ?>
          Thats it, hope you got the idea...

          Comment

          • Shelly

            #6
            Re: Two forms

            It didn't work. Several things:
            1 - I had the target for Aform be _self and for Bform be _blank since it is
            in A form with the _self that the list exists.
            1 - The action path is "", even though you said you put one in. I left it
            that way.
            2 - I assume you meant document.Bform. submit(); and not
            document.Aform. submit();
            3 - If I made the change to Bform (above), then it opened a new window
            (according to Bform), but it didn't pass the value of theList, even though I
            have the B button in A form, and the page that is brought up in that new
            window is the current page, not the redirected one. Aform works fine.
            4 - If I left it at Aform, it doesn't work either.

            Here is the code:
            junk.php
            ======
            <?php
            if (isset($_POST['A'])) {
            $val = $_POST['theList'];
            echo "A0=" . $val . "<br>";
            } else if (isset($_POST['B'])) {
            $val = $_POST['theList'];
            header("Locatio n: junk2.php?val=" . $val);
            exit();
            }
            ?>
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
            <html><head><me ta http-equiv="Content-Type" content="text/html;
            charset=iso-8859-1">
            <title>Untitl ed Document</title></head>

            <form action="" method="post" target="_self" name="Aform">
            <select name="theList">
            <option value="First">F irst</option>
            <option value="Second"> Second</option>
            </select>
            <input type="submit" name="A" value="submit A">
            <input type="button" name="B" value="submit B"
            onClick="docume nt.Bform.submit ()">
            </form>

            <form action="" method="post" target="_blank" name="Bform">
            </form>
            <body></body></html>


            junk2.php
            =======
            <?php
            $val = $_GET['val'];
            ?>
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
            <html><head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <title>Untitl ed Document</title>
            </head>

            <body>
            The value of val is <?php echo $val . "<br>"; ?>
            </body></html>




            "NurAzije" <nurazije@gmail .comwrote in message
            news:1161067525 .892546.259600@ b28g2000cwb.goo glegroups.com.. .
            For AJAX it is so easy, you have a 5 minutes tutorial on this link :

            >
            You can put the second bottun in the first form, like this:
            <form action="" method="POST" name="Aform" target="_blank" >
            <select name="theList">
            <option value="First'"> First </option>
            <option value="Second'" >Second </option>
            </select>
            ....other stuff...
            <input type="submit" value="Form A button" name="A">
            <input type="button" value="Form B button" name="B"
            onclick="docume nt.Aform.submit ();">
            </form>
            You will see I have added onclick="docume nt.Aform.submit ();", and the
            type is button, put an action path for the form, and change the form
            name as I did, every element must have a unique name.
            The php code must look like this:
            <?php
            if (isset($_POST['A'])) {
            $list = $_POST['theList'];
            header('Locatio n: A_target.php?li st=' . $list);
            exit();
            }elseif(isset($ _POST['B'])) {
            $list = $_POST['theList'];
            header('Locatio n: B_target.php?li st=' . $list);
            exit();
            }
            ?>
            Thats it, hope you got the idea...
            >

            Comment

            • Rik

              #7
              Re: Two forms

              Shelly wrote:
              It didn't work. Several things:
              1 - I had the target for Aform be _self and for Bform be _blank since
              it is in A form with the _self that the list exists.
              1 - The action path is "", even though you said you put one in. I
              left it that way.
              2 - I assume you meant document.Bform. submit(); and not
              document.Aform. submit();
              3 - If I made the change to Bform (above), then it opened a new window
              (according to Bform), but it didn't pass the value of theList, even
              though I have the B button in A form, and the page that is brought up
              in that new window is the current page, not the redirected one.
              Aform works fine. 4 - If I left it at Aform, it doesn't work either.
              >
              Here is the code:
              junk.php
              ======
              <?php
              if (isset($_POST['A'])) {
              $val = $_POST['theList'];
              echo "A0=" . $val . "<br>";
              } else if (isset($_POST['B'])) {
              $val = $_POST['theList'];
              header("Locatio n: junk2.php?val=" . $val);
              exit();
              }
              Well, first of all:
              1. Allthough AJAX is a nice buzzword, this has nothing to do with
              (a)synchronous calls with javascript to the server.
              2. Are you absolutely sure you want to rely on javascript for this? In some
              browsers/for some users it will simply not work.
              3. This should actually be in comp.lang.javas cript

              But here you go:
              What I'd do, is have the buttons in the SAME form. Either that, or
              duplicate the selection list if you want a sturdy application.

              junk.php
              <?php

              if(isset($_POST['B']||isset($_POST['A'])){
              if(isset($_POST['B'])) echo 'You choose to open this a new window, but
              this is not possible without javascript enabled';
              $val = $_POST['theList'];
              echo "A0=" . $val . "<br>";
              }
              ?>
              <script type="text/javascript">
              function form_new_window (button){
              var form = button.form;
              form.action = './junk2.php';
              form.method = 'GET'; //or keep it a post
              form.target = '_blank'; //new window
              return true; //just to make sure.
              }
              </script>
              <form action="./junk.php" method="POST" target="_self">
              <select name="theList">
              <option value="First'"> First </option>
              <option value="Second'" >Second </option>
              </select>
              <input type="submit" value="Same window" name="A">
              <input type="submit" value="New window" name="B"
              onclick="form_n ew_window(this) ">
              </form>

              --
              Rik Wasmus


              Comment

              • Shelly

                #8
                Re: Two forms

                Thank you, Rik, very much. This worked.

                Shelly

                "Rik" <luiheidsgoeroe @hotmail.comwro te in message
                news:34ace$4535 7051$8259c69c$2 0768@news2.tude lft.nl...
                Shelly wrote:
                >It didn't work. Several things:
                >1 - I had the target for Aform be _self and for Bform be _blank since
                >it is in A form with the _self that the list exists.
                >1 - The action path is "", even though you said you put one in. I
                >left it that way.
                >2 - I assume you meant document.Bform. submit(); and not
                >document.Aform .submit();
                >3 - If I made the change to Bform (above), then it opened a new window
                >(according to Bform), but it didn't pass the value of theList, even
                >though I have the B button in A form, and the page that is brought up
                >in that new window is the current page, not the redirected one.
                >Aform works fine. 4 - If I left it at Aform, it doesn't work either.
                >>
                >Here is the code:
                >junk.php
                >======
                ><?php
                >if (isset($_POST['A'])) {
                > $val = $_POST['theList'];
                > echo "A0=" . $val . "<br>";
                >} else if (isset($_POST['B'])) {
                > $val = $_POST['theList'];
                > header("Locatio n: junk2.php?val=" . $val);
                > exit();
                >}
                >
                Well, first of all:
                1. Allthough AJAX is a nice buzzword, this has nothing to do with
                (a)synchronous calls with javascript to the server.
                2. Are you absolutely sure you want to rely on javascript for this? In
                some
                browsers/for some users it will simply not work.
                3. This should actually be in comp.lang.javas cript
                >
                But here you go:
                What I'd do, is have the buttons in the SAME form. Either that, or
                duplicate the selection list if you want a sturdy application.
                >
                junk.php
                <?php
                >
                if(isset($_POST['B']||isset($_POST['A'])){
                if(isset($_POST['B'])) echo 'You choose to open this a new window, but
                this is not possible without javascript enabled';
                $val = $_POST['theList'];
                echo "A0=" . $val . "<br>";
                }
                ?>
                <script type="text/javascript">
                function form_new_window (button){
                var form = button.form;
                form.action = './junk2.php';
                form.method = 'GET'; //or keep it a post
                form.target = '_blank'; //new window
                return true; //just to make sure.
                }
                </script>
                <form action="./junk.php" method="POST" target="_self">
                <select name="theList">
                <option value="First'"> First </option>
                <option value="Second'" >Second </option>
                </select>
                <input type="submit" value="Same window" name="A">
                <input type="submit" value="New window" name="B"
                onclick="form_n ew_window(this) ">
                </form>
                >
                --
                Rik Wasmus
                >
                >

                Comment

                • Shelly

                  #9
                  Re: Two forms

                  I was a little too quick in replying. I have four buttons that should not
                  open a new window and two that should. In my actual application I sent it
                  to redir.php in the Javascript. Once there in redir.php, I test on which of
                  the two buttons in the original page activated it and then send it to the
                  proper page.

                  The problem is that even though I only added the onclick to the two buttons
                  for a new page, ALL six buttons go to redir.php AND open a new window. (I
                  have the tests on the four other buttons in the original page.)

                  Can you think of any reason why the buttons that do not have the onclick
                  would execute the Javascript and go to redir.php? I placed the script just
                  inside <bodyand before <form>.

                  Here is the actual code snippet for a button for a new page:

                  <input type="submit" value="Click Here To" name="health"
                  onclick="form_n ew_window(this) "
                  style="color: #E1D2AA; border: 2px solid; border-color:
                  #CD968F #610E08 #400906 #B14F46; background-color: #99160C; font-family:
                  Verdana; font-size: 12px; display: block; height: 25px; width: 148px;
                  font-weight: bold; margin: 3px; padding: 2px; padding-bottom: 5px;
                  vertical-align: middle;" />

                  and here is one for not a new page:

                  <input type="submit" value="Click Here To" name="shop"
                  style="color: #E1D2AA; border: 2px solid; border-color:
                  #CD968F #610E08 #400906 #B14F46; background-color: #99160C; font-family:
                  Verdana; font-size: 12px; display: block; height: 25px; width: 148px;
                  font-weight: bold; margin: 3px; padding: 2px; padding-bottom: 5px;
                  vertical-align: middle;" />

                  and the Javascript is:

                  <body>
                  <script type="text/javascript">
                  function form_new_window (button){
                  var form = button.form;
                  form.action = 'redir.php';
                  form.method = 'POST';
                  form.target = '_blank';
                  return true;
                  }
                  </script>
                  <form ....>


                  Comment

                  • Jerry Stuckle

                    #10
                    Re: Two forms

                    Shelly wrote:
                    I was a little too quick in replying. I have four buttons that should not
                    open a new window and two that should. In my actual application I sent it
                    to redir.php in the Javascript. Once there in redir.php, I test on which of
                    the two buttons in the original page activated it and then send it to the
                    proper page.
                    >
                    The problem is that even though I only added the onclick to the two buttons
                    for a new page, ALL six buttons go to redir.php AND open a new window. (I
                    have the tests on the four other buttons in the original page.)
                    >
                    Can you think of any reason why the buttons that do not have the onclick
                    would execute the Javascript and go to redir.php? I placed the script just
                    inside <bodyand before <form>.
                    >
                    Here is the actual code snippet for a button for a new page:
                    >
                    <input type="submit" value="Click Here To" name="health"
                    onclick="form_n ew_window(this) "
                    style="color: #E1D2AA; border: 2px solid; border-color:
                    #CD968F #610E08 #400906 #B14F46; background-color: #99160C; font-family:
                    Verdana; font-size: 12px; display: block; height: 25px; width: 148px;
                    font-weight: bold; margin: 3px; padding: 2px; padding-bottom: 5px;
                    vertical-align: middle;" />
                    >
                    and here is one for not a new page:
                    >
                    <input type="submit" value="Click Here To" name="shop"
                    style="color: #E1D2AA; border: 2px solid; border-color:
                    #CD968F #610E08 #400906 #B14F46; background-color: #99160C; font-family:
                    Verdana; font-size: 12px; display: block; height: 25px; width: 148px;
                    font-weight: bold; margin: 3px; padding: 2px; padding-bottom: 5px;
                    vertical-align: middle;" />
                    >
                    and the Javascript is:
                    >
                    <body>
                    <script type="text/javascript">
                    function form_new_window (button){
                    var form = button.form;
                    form.action = 'redir.php';
                    form.method = 'POST';
                    form.target = '_blank';
                    return true;
                    }
                    </script>
                    <form ....>
                    >
                    >
                    Shelly,

                    You're probably better off asking javascript questions in a javascript
                    newsgroup, don't you think? :-)

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • Rik

                      #11
                      Re: Two forms

                      Shelly wrote:
                      I was a little too quick in replying. I have four buttons that
                      should not open a new window and two that should. In my actual
                      application I sent it to redir.php in the Javascript. Once there in
                      redir.php, I test on which of the two buttons in the original page
                      activated it and then send it to the proper page.
                      >
                      The problem is that even though I only added the onclick to the two
                      buttons for a new page, ALL six buttons go to redir.php AND open a
                      new window. (I have the tests on the four other buttons in the
                      original page.)
                      >
                      Can you think of any reason why the buttons that do not have the
                      onclick would execute the Javascript and go to redir.php? I placed
                      the script just inside <bodyand before <form>.
                      Ahum, please, pleas, do not use css inline like this. Give the buttons a
                      class and put the layout in an external css-file....
                      <form ....>
                      I'm very curious what your <formtag actually sais, because I've switched
                      them around, defaulting to _self instead of _blank.... That would be the
                      easy solution :-)

                      If not, could you provide a link of the form in action perhaps? (or the
                      exact file)

                      Grtz,
                      --
                      Rik Wasmus


                      Comment

                      • Shelly

                        #12
                        Re: Two forms


                        "Rik" <luiheidsgoeroe @hotmail.comwro te in message
                        news:88b95$4535 9e36$8259c69c$2 4588@news2.tude lft.nl...
                        Shelly wrote:
                        >I was a little too quick in replying. I have four buttons that
                        >should not open a new window and two that should. In my actual
                        >application I sent it to redir.php in the Javascript. Once there in
                        >redir.php, I test on which of the two buttons in the original page
                        >activated it and then send it to the proper page.
                        >>
                        >The problem is that even though I only added the onclick to the two
                        >buttons for a new page, ALL six buttons go to redir.php AND open a
                        >new window. (I have the tests on the four other buttons in the
                        >original page.)
                        >>
                        >Can you think of any reason why the buttons that do not have the
                        >onclick would execute the Javascript and go to redir.php? I placed
                        >the script just inside <bodyand before <form>.
                        >
                        Ahum, please, pleas, do not use css inline like this. Give the buttons a
                        class and put the layout in an external css-file....
                        >
                        ><form ....>

                        Will do. right now, I just wanted to get ti to work.

                        >
                        I'm very curious what your <formtag actually sais, because I've switched
                        them around, defaulting to _self instead of _blank.... That would be the
                        easy solution :-)
                        It defaults to "_self"

                        <form action="" method="POST" name="pets" target="_self">

                        Shelly


                        Comment

                        • Shelly

                          #13
                          Re: Two forms

                          The solution that worked (thanks to Gleep) was to add:

                          onchange="windo w.document.form B.theListH.valu e=(this.options[this.selectedIn dex].text)"

                          to a change in the drop-down list (theList in formA). I set theListH as a
                          hidden variable in the other form (formB).

                          I test on all the buttons and the ones that were from formB I use the value
                          from theListH and the ones from the first form I use the value from theList.
                          Of course, I initially set theListH to be the first one on the drop-down
                          list.

                          Shelly


                          Comment

                          • Rik

                            #14
                            Re: Two forms

                            Shelly wrote:
                            "Rik" <luiheidsgoeroe @hotmail.comwro te in message
                            news:88b95$4535 9e36$8259c69c$2 4588@news2.tude lft.nl...
                            >Shelly wrote:
                            >>I was a little too quick in replying. I have four buttons that
                            >>should not open a new window and two that should. In my actual
                            >>application I sent it to redir.php in the Javascript. Once there in
                            >>redir.php, I test on which of the two buttons in the original page
                            >>activated it and then send it to the proper page.
                            >I'm very curious what your <formtag actually sais, because I've
                            >switched them around, defaulting to _self instead of _blank.... That
                            >would be the easy solution :-)
                            >
                            It defaults to "_self"
                            >
                            <form action="" method="POST" name="pets" target="_self">
                            Any change on getting the whole script + form? My example works perfectly
                            here, and I cannot guess what went wrong in your form.
                            --
                            Grtz

                            Rik Wasmus


                            Comment

                            • Shelly

                              #15
                              Re: Two forms


                              "Rik" <luiheidsgoeroe @hotmail.comwro te in message
                              news:8a0c6$4536 3e5d$8259c69c$1 2862@news2.tude lft.nl...
                              Shelly wrote:
                              >"Rik" <luiheidsgoeroe @hotmail.comwro te in message
                              >news:88b95$453 59e36$8259c69c$ 24588@news2.tud elft.nl...
                              >>Shelly wrote:
                              >>>I was a little too quick in replying. I have four buttons that
                              >>>should not open a new window and two that should. In my actual
                              >>>applicatio n I sent it to redir.php in the Javascript. Once there in
                              >>>redir.php, I test on which of the two buttons in the original page
                              >>>activated it and then send it to the proper page.
                              >>I'm very curious what your <formtag actually sais, because I've
                              >>switched them around, defaulting to _self instead of _blank.... That
                              >>would be the easy solution :-)
                              >>
                              >It defaults to "_self"
                              >>
                              ><form action="" method="POST" name="pets" target="_self">
                              >
                              Any change on getting the whole script + form? My example works perfectly
                              here, and I cannot guess what went wrong in your form.
                              Not a problem getting the script, but I fixed it a different way. I had
                              posted this to this group and to alt.comp.lang.p hp. Gleep answered there
                              and that led me in the right track. What I did was use javascript with
                              onchange in formA's drop-=down list to set a hidden variable in formB.
                              Then when I hit a submit button in formB, I read the value of that hidden
                              variable. it works like a charm -- and I can use that to do other things as
                              well in other code.

                              Thank you four your help and interest.

                              Shelly

                              P.S. This is a great list. I am greatly indebted to this list, and will
                              contribute more as I can find even a little time.


                              Comment

                              Working...