Passing variable from javascript to servlet!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinodsk101
    New Member
    • Oct 2008
    • 9

    Passing variable from javascript to servlet!!!

    Hi all,
    I have one doubt,
    can we pass a variable from javascript to servlet???

    In Brief,
    i am getting some values in javascript through jsp and the same values i
    want in servlet. Ya i know i can get this values in servlet by using request.getPara meter("");
    But thise values are dynamically generated so i dont know how many values will be. so i am planning to put some array list so that i can forward this values to servlet.

    but i can't get this array which is declared in javascript!! :( so any buddy can help me how to get this dynamically generated values in servlet!!!

    Thanks in Advance!! :)
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You would get an array (or values in a select) using request.getPara meterValues instead.

    Comment

    • vinodsk101
      New Member
      • Oct 2008
      • 9

      #3
      Hi,

      But if i declare a array in javascript how can i get that array in servlet???
      Is there any way to declare array in javascript/html and same we can use in servlet??

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        You could just set the values to a hidden checkbox on the form and submit that.

        Comment

        • vinodsk101
          New Member
          • Oct 2008
          • 9

          #5
          Hi,
          Actually i have a dynamic table, i have add button to table which will add the new row to table, i want all the table values in servlet for my further use.
          i used hidden select box,and i am adding all values to hidden selectbox, but problem is that i am getting only fist value, but if i give more than 2 or 3 rows, i am not receiving all values in servlet.

          But if i use hidden checkbox how can i put all values in single checkbox value??

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            The hidden select box approach should work as well. Post the code you used to populate the select.

            Comment

            • vinodsk101
              New Member
              • Oct 2008
              • 9

              #7
              Hi,
              I used this code to populate the select box.

              [code=javascript]var k;
              for(k=1; k<=lastRow; k++)
              {
              var newOption = new Option();
              var x=document.getE lementById('sel Row'+ k).selectedInde x;
              var y=document.getE lementsByTagNam e("option");

              alert(y[x].text+" In for loop has the index of: "+y[x].index);

              newOption.text = y[x].text;
              newOption.value = y[x].text;
              document.table. ListBox4.option s[len2] = newOption;
              len2=len2+1;

              }[/code]

              But some of the values are not inserting propertly!!
              Last edited by acoder; Nov 25 '08, 09:51 AM. Reason: A

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Line 6 (setting y) will cause problems because you're getting all the options in the document (which is increasing all the time in the loop). So put that statement outside the loop.

                Comment

                Working...