0

I have this code:

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

// converts to JSON the array and returns a string.
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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.