Question

[Solved] Sending multiple data parameters with jQuery AJAX

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 🙂

Enquirer: JamesG

||

Solution #1:

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 }
Respondent: Linus Thiel

Solution #2:

you should set your data like so :

data: 'code="+code+"&userid='+userid
Respondent: Camille Hodoul

Solution #3:

you can try this :

data: 'code="+code+"&userid='+userid,

instead of

data: 'code="+code+"userid='+userid,
Respondent: Poonam

Solution #4:

usually sending your data like this helps:

data: { code: code, userid: userid }

the most important thing to not forget is to verify if the name of the variables you are sending are the same in the server side

Respondent: break7533

Solution #5:

The answer from Linus Gustav Larsson Thiel refers, I used the following &.ajax() that triggers when a button is clicked and it works great. I could pass day, month and year parameters.

$('#convertbtn').on('click',function(){
ageddajax = $("#agedd").val();
agedmmajax = $("#agemm").val();
ageyyyyajax = $("#ageyyyy").val();
    if(ageddajax > 0 && agemmajax > 0 && ageyyyyajax >0){
    $.ajax({
        type:'POST',
        url:'ajaxDataAge.php',
        data:'agedd_id='+ageddajax +'&agemm_id='+agemmajax +'&ageyyyy_id='+ageyyyyajax,
            success:function(html){
            $('#cydivage').html(html);
            }
        });
    }   
});
Respondent: Sanopotens

Solution #6:

Try this code… it is working for me …

<script type='text/javascript'>
$(document).ready(function(){
  $(".star").click(function(){
   var rate_value1= $(this).index(".star")+1;
    $.ajax({
    type: "POST",
    dataType: "json",
    url:  "<?php echo(rootpath()) ?>/vote.php",

data: { product_id: '<?php echo($product_id_to_permalink) ?>' , rate_value: rate_value1 }

        });
      });
    });
</script>       
Respondent: waheed zulfiqar

Solution #7:

 Try this code... 
    <script>
     function quote_ajax_table(){
       var doc_name = '<?php echo $test; ?>';
       var doc_no = '<?php echo $doc_no; ?>';

$.get('quote_ajax_table.php?doc_no='+doc_no+'&doc_name="+doc_name,function(data) {
   $(".dyna').html(data);
 });
}
</script>

//in html code 
  <div class="dyna"></div>
Respondent: Dee_wab

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy