Francisco Garcia
Francisco Garcia

Reputation: 5

Jquery Load and send variables to url

I use the function "load" of Jquery for load page , in this url i send by "load" some values as this example :

jQuery("#load_data").show(1000).load("index.php?send_values="+datasend+"");

The value "datasend" get these values from function javascript as this :

    function getvalues()
    {
    var getdata="value 1";
    getdata +="value 2";
    getdata +="value 3";

var datasend=getdata;
    }

If insert alert inside funtion i get all values , for example :

        function getvalues()
        {
        var getdata="value 1";
        getdata +="value 2";
        getdata +="value 3";

    var datasend=getdata;

alert("Show"+datasend);
        }

But if i use load of Jquery , this no works :

       function getvalues()
        {
        var getdata="value 1";
        getdata +="value 2";
        getdata +="value 3";

    var datasend=getdata;

jQuery("#loader").load("index.php?send_values="+datasend);
        }

The other side in the page i want send/get values i have code in PHP , but when i do :

<?php echo $_REQUEST['send_values'];?>

Only get the first data in this case "value 1" and no all values , but with javascript "alert" i get all values but load of jquery no send all values by URL for finally get all values

Thank´s for the help , regards

Upvotes: 0

Views: 875

Answers (2)

Guffa
Guffa

Reputation: 700152

If you check again, you will see that the data that arrives at the server isn't "value 1", but only "value".

A correct URL can't contain spaces, so the load method uses space as a separator between the URL and an optional selector to get a fragment from the loaded data. As there are spaces in the value in datasend, only the part up to the first space is included in the URL, the rest is interpreted as a selector.

You would use the encodeURIComponent method to encode the value so that it can be used in the URL:

jQuery("#loader").load("index.php?send_values=" + encodeURIComponent(datasend));

Alternatively you can use the data parameter of the load method to include the data:

jQuery("#loader").load("index.php", { send_values: datasend });

Upvotes: 1

Shahar
Shahar

Reputation: 1677

Try doing:

var datasend=encodeURIComponent(getdata);

Upvotes: 0

Related Questions