开发者

Passing JSON-ed array to PHP with $.Post

开发者 https://www.devze.com 2023-01-30 05:04 出处:网络
I have this code: $(\"#sendData\").click(function(e) { // converts to JSON the array and returns a st开发者_C百科ring.

I have this code:

$("#sendData").click(function(e) {

// converts to JSON the array and returns a st开发者_C百科ring.
var updateValues = JSON.stringify(dataArray); 

$.post("test.php", updateValues, function(data){
    document.write(alert)});
});

As you may see, is my intention to send a JSON array to test.php. Now in test.php i have something like:

<?php

    if(isset($_POST["updateValues"])) {
        echo $_POST["updateValues"];
    } else {
        echo "Error."
    }

?>

Now, i'm getting "Error". I believe it's because the array can not be passed just that way, even if it's JSON-ed. What's the correct way of passing arrays to PHP scripts?


Try...

$.post("test.php", {'updateValues': updateValues}, function(data){
        alert(data);
});

You had a few syntax issues - and each post variable should have a key in the object to access it in PHP like that.

0

精彩评论

暂无评论...
验证码 换一张
取 消