Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send string with two parameters

I have this code, but this line has some problem.

 var dataString = 'name='+name&'id='+id;

what is sent (firebug):

'id ' id
'name' name

The line above works correctly if i do: var dataString = 'name='+name; However, i need to pass two parameters. What is the correct way to do that?

code

 <script type="text/javascript">
    $(function () {
        $(".vote").click(function () {
            var id = $(this).attr("id");
            var name = $(this).attr("name");
            var dataString = 'name='+name&'id='+id;

            if (name == 'up') {
                $.ajax({
                    type: "POST",
                    url: "url.php",
                    data: dataString,
                    cache: false,
                    success: function (html) {

                    }
                });
            return false;
        });
    });
    </script>
like image 612
daniel__ Avatar asked Jan 18 '23 23:01

daniel__


2 Answers

You should do:

 var dataString = { name: name, id: id}

instead of

 var dataString = 'name='+name&'id='+id;

So that you are sure that the supplied values are correctly URI encoded.

like image 176
Nicola Peluchetti Avatar answered Jan 20 '23 12:01

Nicola Peluchetti


Try this:

var dataString = 'name='+name+'&id='+id;

Instead of

var dataString = 'name='+name&'id='+id;

The & should be inside '', and you need extra + to concat "name" variable and '&id=' string. So this should work.

UPD:

You can also do:

var dataString = { name: name, id: id }
like image 44
SlavaNov Avatar answered Jan 20 '23 13:01

SlavaNov