简体   繁体   English

AJAX将变量从javascript传递到PHP

[英]AJAX pass variable from javascript to PHP

I am passing a variable from javascript to another PHP page via ajax. 我通过ajax将变量从javascript传递到另一个PHP页面。 I am not able to do it. 我做不到。 This is the code I have so far. 这是我到目前为止的代码。

<script>
function loadXMLDoc()
        {
        $.ajax({
                    type: "POST",
                    url: "update.php",
                    data: { value : masterdata },
                    success: function(data)
                    {
                        alert("success!");
                    }
                }); 

}
$("button").on('click',function(){ loadXMLDoc(); });
</script>

The masterdata variable comes from another javascript function in which I have declared the variable globally, as in this below function. masterdata变量来自另一个javascript函数,在其中我已全局声明了该变量,如下面的函数所示。

<script>

    var masterdata;      
    $("[data-slider]")

       .each(function () {
           var range;
            var input = $(this);
            $("<span>").addClass("output")
                .insertAfter(input);
            range = input.data("slider-range").split(",");
            $("<span>").addClass("range")
                .html(range[0])
                .insertBefore(input);
            $("<span>").addClass("range")
                .html(range[1])
                .insertAfter(input);
        })
        .bind("slider:ready slider:changed", function (event, data) {
            $(this).nextAll(".output:first")
                .html(data.value.toFixed(2));
                masterdata = data.value;

        });


</script>

In my update.php file, I am trying to access the variable using $_REQUEST . 在我的update.php文件中,我尝试使用$ _REQUEST访问变量。 The code is as below. 代码如下。

<?php

    $uid = $_REQUEST['value'];
    echo "Am I getting printed";
    echo $uid;
    // Do whatever you want with the $uid
?>

However, if I click on Update button nothing happens. 但是,如果我单击“ 更新”按钮,则什么也不会发生。 I just get an alert saying Success for my ajax call. 我刚收到一个警告,说我的ajax呼叫成功。 Can someone please help me out? 有人可以帮我吗?

in your success function, you're not asking it to display the returned data that's why you are only seeing the alert, change it to: 在成功函数中,您并没有要求它显示返回的数据,这就是为什么您仅看到警报的原因,请将其更改为:

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

because data here is the values returned from your request 因为这里的data是您的请求返回的值

and for the php part, keep in mind one thing, just adjust your php page to display the data you need, and remember it doesn't matter how you make this data, with echo or just plain html, the result of the php code is what you will get in your variable in ajax. 对于php部分,请记住一件事,只需调整您的php页面以显示所需的数据即可,请记住,使用echo或纯html生成此数据是无关紧要的,PHP代码的结果是您将在ajax中的变量中得到的。 and if you go JSON, you can do in the php page: 如果使用JSON,则可以在php页面中执行以下操作:

$uid = $_REQUEST['value'];
$x.uid = $uid;
$x.something = "something";
return $x;

this way you will have all the data you need on client side as an object and then you can manipulate it the way you like, in a much more compact way, just search for RESTful api if you want to read more about the topic 这样,您就可以将客户端所需的所有数据作为一个对象,然后可以按照自己喜欢的方式以一种更紧凑的方式对其进行操作,如果您想了解有关该主题的更多信息,只需搜索RESTful api

I would recommend JSON-encoding the output you have obtained from your update.php script, and pass it back to the AJAX function. 我建议您对从update.php脚本获得的输出进行JSON编码,然后将其传递回AJAX函数。 In your update.php file, you can simply use the PHP function json_encode($var) , and echo it: 在您的update.php文件中,您可以简单地使用PHP函数json_encode($var)并对其进行回显:

<?php
    $uid = $_REQUEST['value'];
    echo json_encode($uid);
?>

From your AJAX function, you should specify the dataType (usually JS will intelligently guess the type of data returned, but we should always specify it just in case): 在AJAX函数中,您应该指定dataType (通常JS会智能地猜测返回的数据类型,但是为了以防万一,我们应该始终指定它):

function loadXMLDoc() {
    $.ajax({
        type: "POST",
        url: "update.php",
        data: { value : masterdata },
        dataType: "json",
        success: function(data) {
           // Log data into console for reference (and checking, perhaps)
           console.log(data);

           // Parse data here
        }
    }); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM