2

I am trying to send data from Jquery to PHP. This is what I have:

var jsonArray = JSON.stringify(dataArray);

            $.ajax({
                type: "POST",
                url: "addcar_details.php",
                data: {data : jsonArray},
                dataType: "json",
                success: function(data) {
                    console.log("sent");
                }
            });

I am trying to see what I have got in the "$_POST" from the "addcar_details.php" file. It is empty and I am not sure what I am missing for this to be working.

Thanks in advance :)

2 Answers 2

1

Try in this way

var jsonArray = {field1:'Field 1 Value',field2:'Field 2 Value'};

$.ajax({
    method: "POST",
    url: "addcar_details.php",
    data: jsonArray,
    success: function(data) {
        console.log(data);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

You should use method: 'POST'

$.ajax({
    method: "POST",
    url: "addcar_details.php",
    data: {'data' : jsonArray},
    dataType: "json",
    success: function(data) {
        console.log("sent");
        console.log(data);

    },
    error: function() {
        console.log("there was some error");
    }
});

Read more here.

6 Comments

I tried this, and now I don't even get the console.log() so assuming its failing
You can add the error callback. Check the update in my answer.
And if you are using the console.log - you better check the network tab in the developers tool bar to see what exactly was the request and what the server's response was. Are you sure your php file is not the problem here?
So it is going in the error, I checked the network tab and it is posting the array and looks fine. All i have in the php file is just a echo to see what's in the post "$_POST"
Are you sure the response from the server is a valid json string?
|

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.