php 使用 jQuery AJAX 发送多个数据参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9328743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:34:50  来源:igfitidea点击:

Sending multiple data parameters with jQuery AJAX

phpjqueryajax

提问by JamesG

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

我正在向 php 文件发送 ajax 请求,如下所示:

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.

问题是用户 id php 变量没有被 ajax 发送到 check_code.php 页面。或者至少我似乎无法将 id 回显到页面上。

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.

这是将多个值传递给服务器端页面的正确方法吗?如果没有传递用户 ID,只需传递代码就可以正常工作。

Thanks guys :)

谢谢你们 :)

回答by Linus Gustav Larsson Thiel

Here is how POSTdata should be formatted:

以下是POST应如何格式化数据:

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:

但是如果您将数据指定为对象,jQuery 会为您做到这一点:

data: { code: code, userid: userid }

回答by Camille Hodoul

you should set your datalike so :

你应该这样设置data

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

回答by Poonam

you can try this :

你可以试试这个:

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

instead of

代替

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

回答by Sanopotens

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.

来自 Linus Gustav Larsson Thiel 的回答提到,我使用了以下 &.ajax(),它在单击按钮时触发并且效果很好。我可以传递日、月和年参数。

$('#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);
            }
        });
    }   
});

回答by break7533

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

不要忘记的最重要的事情是验证您发送的变量的名称在服务器端是否相同

回答by Dee_wab

 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>

回答by waheed zulfiqar

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>