Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

php - 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 :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...