Which button was clicked - JSP with JavaScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jambyl
    New Member
    • Oct 2007
    • 8

    Which button was clicked - JSP with JavaScript

    I have two JSP pages PageOne.jsp and PageTwo.jsp.
    On the PageOne.jsp I have TextBoxOne and TextBoxTwo.
    Next to each of these textboxes there is a "Wizard1" button and "Wizard2" button.
    When the user clicks any of these "Wizard" buttons it will take him to PageTwo.jsp by popping up a new jsp window (PageTwo.jsp).
    On this PageTwo.jsp the user will need to fill out a form and then click "Done" button.
    After he clicks the "Done" button I need to close the PageTwo.jsp, and take the user back to the PageOne.jsp and populate one of the textboxes (depending on which Wizard Button was clicked).

    One of the Wizard buttons already works and populates the TextBoxOne. Now I need to get the second Wizard botton working without creating a new JSP page and a new servlet.

    I'm thinking about puting an IF stetement on to my PageTwo.jsp, which will look something like this:


    if (Wizard1 was clicked)
    {
    Populate TextBoxOne
    }

    If (Wizard2 was clicked)
    {
    Polulate TextBoxTwo
    }

    Now, my question is, I want to know what is the syntax for writing this IF statement? How can I find out which button was clicked?


    Thanks,
  • jambyl
    New Member
    • Oct 2007
    • 8

    #2
    I tried doing something like this, but it is not working:

    if (document.PageO neForm.button.n ame == 'Wizard1') {
    opener.document .PageOneForm.Te xtBoxOne.value = "1";
    close();
    return true;
    }

    Thanks

    Comment

    • Dasty
      Recognized Expert New Member
      • Nov 2007
      • 101

      #3
      Just remember the last pressed button number in global variable in the first page.

      [HTML]<script language="javas cript">
      var number_of_butto n_i_clicked = null;
      </script>[/HTML]

      In onclick event of the buttons you'll set that variable like:

      [code=javascript]function click_handler_f or_button_1()
      {
      var new_window;
      number_of_butto n_i_clicked = 1;
      new_window = window.open('Pa geTwo.jsp', [your properties]);
      }[/code]

      and finally your IF code in page 2 will look like:

      [code=javascript]if (opener.number_ of_button_i_cli cked == 1)
      {
      [populate box 1]
      }
      .. etc
      [/code]

      or are you looking for something different?

      Comment

      • jambyl
        New Member
        • Oct 2007
        • 8

        #4
        Thank you for your reply!
        Could you please explain what "parent" means? What should I write in my case? How is it going to know what is the value of the "number_of_butt on_i_clicked" is?

        if (parent.number_ of_button_i_cli cked == 1)
        {
        [populate box 1]
        }
        .. etc
        }

        BTW, I'm not using PHP. I am using JavaScript inside of a JSP page.

        Thanks,
        Jambyl

        Comment

        • Dasty
          Recognized Expert New Member
          • Nov 2007
          • 101

          #5
          It's not "parent" it's "opener" (sorry). And those codes are javascript (not php) - again, my fault - all fixed (I am kind of retard).

          number_of_butto n_i_clicked is global variable in PageOne, and you can read its value from PageTwo by using opener.number_o f_button_i_clic ked reference.

          Comment

          • jambyl
            New Member
            • Oct 2007
            • 8

            #6
            Dasty,
            Thank you for your help! It works like a champion!!! :))

            If you don't mind, I have one more question to ask you.

            I don't know how to do the following:

            After I populate the TextBoxOne (using our Wizard1 button), I want the value in TextBoxTwo to automatically appear three times the value of TextBoxOne.
            For example if the value that we got in TextBoxOne is 3, I want the value in TextBoxTwo to be automatically become 9.

            This is not going to be the case for TextBoxTwo. That means If we populate the value in TextBoxTwo (using out Wizard2 button), we don't want anything to show in TextBoxOne.

            Thank you in advance!

            Jambyl

            Comment

            • Dasty
              Recognized Expert New Member
              • Nov 2007
              • 101

              #7
              I dont understand, who can enter values in TextBoxOne and TextBoxTwo? Are you doing it only by your js code? Or are the users free to enter their values into those boxes?

              1) If you are entering values into those boxes just from that mentioned PageTwo window, why dont you set both values in same function? like:

              [CODE=javascript]opener.document .PageOneForm.Te xtBoxOne.value = new_value;
              opener.document .PageOneForm.Te xtBoxTwo.value = new_value * 3;[/CODE]

              2) if the users enter the values, you should use onchange event to count new value in second textbox like:

              Code:
              <input type=text name=TextBoxOne id=TextBoxOne onchange="count_second_box(this);">
              
              function count_second_box(obj)
              {
                document.PageOneForm.TextBoxTwo.value = this.value * 3;
              }
              This code is very simplified (no format checks .. etc) just to give you an idea.

              Comment

              • jambyl
                New Member
                • Oct 2007
                • 8

                #8
                This is great! Thank you!!!
                I appreciate your help!!!

                Kind regards,
                Jambyl

                Comment

                Working...