Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending multiple data parameters with jQuery AJAX

Tags:

jquery

ajax

php

I am sending an ajax request to a php file as shown here:

function checkDB(code, userid) {    $.ajax({   type: "POST",   url: "<?php bloginfo('template_url'); ?>/profile/check_code.php",   data: 'code='+code+'userid='+userid,   datatype: "html",   success: function(result){         if(result == 0)         {             $('#success').html( code + ' has been redeemed!');             // alert('success');//testing purposes         }         else if(result == 2)         {             $('#err').html(  code + ' already exists and has already been redeemed....');             //alert('fail');//testing purposes         }else if(result == 1){             $('#err').html(  code + ' redeem code doesnt exist');               }          alert(result);             }   })  } 

This is sent calling the function on submit, like so:

<form method="post" class="sc_ajaxxx" id="sc_add_voucherx" name="sc_ajax"        onsubmit="checkDB(document.sc_ajax.sc_voucher_code.value, <?php echo $user_id ?>); return false;"> </form> 

The problem is that the user id php variable is not getting sent to the check_code.php page by ajax. or at least I cant seem to echo the id back to the page.

Is this the correct way of passing multiple values to a server-side page? Without the userid passing over, it works fine just passing over the code.

Thanks guys :)

like image 713
JamesG Avatar asked Feb 17 '12 13:02

JamesG


People also ask

How pass multiple parameters in URL jQuery Ajax in php?

function getInfo(val1, val2) { $. ajax({ type: "POST", url: "get_info. php", data: 'value1='+val1+'&value2'+val2, success: function(data){ $("#info"). html(data); } }); };

How pass multiple parameters in URL using jQuery?

Redirecting with multiple Parameters using jQuery When the Send Button is clicked, the values of the TextBox and DropDownList are fetched and are encoded using the JavaScript encodeURIComponent function and added as QueryString parameters to a URL. Finally, the page is redirected to the URL.


2 Answers

you should set your data like so :

data: 'code='+code+'&userid='+userid 
like image 30
Camille Hodoul Avatar answered Sep 21 '22 09:09

Camille Hodoul


Here is how POST data should be formatted:

key1=value1&key2=value2&key3=value3 

In your case (note the & as a separator):

'code=' + code + '&userid=' + userid 

But jQuery does that for you if you specify your data as an object:

data: { code: code, userid: userid } 
like image 173
Linus Thiel Avatar answered Sep 19 '22 09:09

Linus Thiel