在php中不能回显jquery(使用ajax)变量吗

时间:2022-06-11 09:01:26

I'm trying to post a variable through ajax. But it won't echo the variable in php.

我试图通过ajax发布一个变量。但是它不会在php中回显变量。

This are the variables (these are working, seen in the log):

这是变量(在日志中可以看到):

 $("#slider").bind("valuesChanged", function(e, data){console.log("min: " + data.values.min + " max: " + data.values.max);});

The Ajax part:

Ajax的部分:

$("#slider").bind("valuesChanged", function (e, data) {

  $.ajax({
   type: "POST",
   dataType: "text",
   url: "../test.php",
   data: { minValue: data.values.min, maxValue: data.values.max },
   async: false,
   success: function(data){
      alert('yeah')

    },
     error: function(xhr) {
             alert('fail') // if your PHP script return an erroneous header, you'll land here
            }
 });
});

</script>

And php echo:

和php echo:

<?php

if ( $_POST ) {

        echo $_POST[ 'minValue' ];
            }
?>

Now why does it not echo the post? Thanks!

为什么它不回应这个帖子呢?谢谢!

4 个解决方案

#1


1  

When you do echo in your PHP script it will be sent back to the ajax call as a response. So you must check in the success part of the $.ajax. So do,

当您在PHP脚本中执行echo时,它将作为响应发送回ajax调用。因此,您必须检查$.ajax的成功部分。所以做的,

....
success: function(data) {
  alert(data);
},
....

#2


1  

Ok seeing as my comment turned out to be the answer, here goes:
Change the success callback to do something with the data:

好吧,既然我的评论被证明是正确的答案,那就改变success回调来处理数据:

success: function(data)
{
    console.log(data);
}

I'd also recommend not setting the dataType explicitly. jQ does a good job at figuring out what the response's datatype is, and when sending an object litaral (like you're doing) it also does a smashing job at dealing with that, too.

我还建议不要显式地设置数据类型。jQ在确定响应的数据类型方面做得很好,在发送对象litaral(就像您正在做的那样)时,它在处理这个问题上也做得很出色。

Check this question for more details on how jQ "guesses" the datatype of the resonse
As you can read in the API docs, the default datatype is application/x-www-form-urlencoded; charset=UTF-8, so sending an object literal is no problem at all.

请检查这个问题,以获得更多关于jQ如何“猜测”共振的数据类型的细节,正如您在API文档中所看到的,默认的数据类型是application/ www-form- urlencodes;charset=UTF-8,因此发送一个对象文字完全没问题。

#3


1  

The echo result is returned in the data variable passed to success method. It won't appear unless you do

回波结果在传递给success方法的数据变量中返回。除非你这么做,否则它不会出现

alert(data)

#4


0  

$("#slider").bind("valuesChanged", function(e, data) {
    $.ajax({
        type: "POST",
        dataType: "text",
        url: "../test.php",
        data: { minValue: data.values.min, maxValue: data.values.max },
        async: false,
        success: function(data) {
//          alert('yeah')
            alert(data)
        },
        error: function(xhr) {
            alert('fail') // if your PHP script return an erroneous header, you'll land here
        }
    });
});

#1


1  

When you do echo in your PHP script it will be sent back to the ajax call as a response. So you must check in the success part of the $.ajax. So do,

当您在PHP脚本中执行echo时,它将作为响应发送回ajax调用。因此,您必须检查$.ajax的成功部分。所以做的,

....
success: function(data) {
  alert(data);
},
....

#2


1  

Ok seeing as my comment turned out to be the answer, here goes:
Change the success callback to do something with the data:

好吧,既然我的评论被证明是正确的答案,那就改变success回调来处理数据:

success: function(data)
{
    console.log(data);
}

I'd also recommend not setting the dataType explicitly. jQ does a good job at figuring out what the response's datatype is, and when sending an object litaral (like you're doing) it also does a smashing job at dealing with that, too.

我还建议不要显式地设置数据类型。jQ在确定响应的数据类型方面做得很好,在发送对象litaral(就像您正在做的那样)时,它在处理这个问题上也做得很出色。

Check this question for more details on how jQ "guesses" the datatype of the resonse
As you can read in the API docs, the default datatype is application/x-www-form-urlencoded; charset=UTF-8, so sending an object literal is no problem at all.

请检查这个问题,以获得更多关于jQ如何“猜测”共振的数据类型的细节,正如您在API文档中所看到的,默认的数据类型是application/ www-form- urlencodes;charset=UTF-8,因此发送一个对象文字完全没问题。

#3


1  

The echo result is returned in the data variable passed to success method. It won't appear unless you do

回波结果在传递给success方法的数据变量中返回。除非你这么做,否则它不会出现

alert(data)

#4


0  

$("#slider").bind("valuesChanged", function(e, data) {
    $.ajax({
        type: "POST",
        dataType: "text",
        url: "../test.php",
        data: { minValue: data.values.min, maxValue: data.values.max },
        async: false,
        success: function(data) {
//          alert('yeah')
            alert(data)
        },
        error: function(xhr) {
            alert('fail') // if your PHP script return an erroneous header, you'll land here
        }
    });
});