Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing and retrieving multiple parameters in jquery from one page to another page

I want to pass 4 names using jQuery from one page to another page using jQuery in winRT app. done using one parameter. How to pass multiple parameters and retrieve in 2nd page?

Coding for one parameter.

default.html:

<html>
<head>
<script>
       $(document).ready(function () {
        $(function () {
            $('#navigate12').click(function () {
                var te = $("#text1").val();
                var tea = $("#text2").val();
                 window.location.href = "/page.html?cal=" + te; 
                   });
        });});
</script>
</head>
<body>
 <button id="navigate12">navigate</button>
 <input id="text1" type="text"/>
 <input id="text2" type="text"/>
 <input id="text3" type="text"/>
 <input id="text4" type="text"/>
</body>
</html>

 page.html
<html>
<head>
      <script src="/js/jquery-1.9.1.js"></script>
      <script>
              $(document).ready(function () {                  
                function qs() {
                var qsparam = new Array();
                var query = window.location.search.substring(1);
                var parms = query.split('&');
                for (var i = 0; i < parms.length; i++) {
                    var pos = parms[i].indexOf('=');
                    if (pos > 0)
                    {
                        var key = parms[i].substring(0, pos);
                        var val = parms[i].substring(pos + 1);
                        qsparam[key] = val;
                    }

                }
               var msg = new Windows.UI.Popups.MessageDialog(val);
               msg.showAsync();
             } 
               var splitstr = qs();
                    });
</script>
<title>
</title>
</head>
<body>

</body>
</html>
like image 988
Adi Avatar asked Dec 03 '22 23:12

Adi


2 Answers

You can pass GET parameters to a page using "?param=foo" for the first parameter. Additional parameters are included with "&" instead of "?".

Since you are passing GET parameters, you can put them all in the URL and read them in your target page. Alternatively, you can use POST parameters instead.

Here's a reference on GET vs PUT parameters:

http://www.w3schools.com/tags/ref_httpmethods.asp

If you are passing all parameters as GET, you can expand this bit:

var te = $("#text1").val();
var tea = $("#text2").val();
window.location.href = "/page.html?cal=" + te; 

to something like this:

var te = $("#text1").val();
var text2 = $("#text2").val();
var text3 = $("#text3").val();
var text4 = $("#text4").val();
window.location.href = "/page.html?cal=" + te + "&text2=" + text2 + "&text3=" + text3 + "&text4=" + text4;

As for obtaining the parameters in the other page: I am assuming that you want to obtain the GET parameters in the other page via jQuery, since your target page is plain HTML. If this is the case, check this question

like image 89
Filippos Karapetis Avatar answered Dec 28 '22 21:12

Filippos Karapetis


done using this technique in default.html

$(function () {
               $('#navigate12').click(function () {
                var te = $("#text1").val();
                var text2 = $("#text2").val();
                var text3 = $("#text3").val();
                var text4 = $("#text4").val();
                window.location.href = "/page.html?cal=" + te + "&text2=" + text2 +
               "&text3=" + text3 + "&text4=" + text4; <-- this line has changes to include multiple parameters rest is same as in the question

and in page.html

    <script>
              $(document).ready(function () {                  
              function qs() {
                var qsparam = new Array(10);
                var query = window.location.search.substring(1);
                var parms = query.split('&');
               for (var i = 0; i < parms.length; i++) {
               var pos = parms[i].indexOf('=');
               if (pos > 0)
              {
                        var key = parms[i].substring(0, pos);
                        var val = parms[i].substring(pos + 1);
                        qsparam[i] = val;    
              }                          
                                                       }
               text1.value = qsparam[0];
               text2.value = qsparam[1];
               text3.value = qsparam[2];
               text4.value = qsparam[3];



              }
              var splitstr = qs();



                    });
    </script>
    <title></title>
</head>
<body>
   <input id="text1" type="text" />
    <input id="text2" type="text" />
   <input id="text3" type="text" />
    <input id="text4" type="text" />
 </body>
 </html>

we are calling the function qs() using

var splitstr = qs();

it splits the parameters and to see the value of different variable u can use text1.value=parms if any doubt u can ask :-)

like image 20
Adi Avatar answered Dec 28 '22 22:12

Adi