I have a javascript object like below
var customerinfo = {
customer_name : $('#customer_name').val(),
customer_address: $('#customer_address').val(),
customer_city : $('#customer_city').val(),
customer_state : $('#customer_state').val(),
customer_zip : $('#customer_zip').val(),
};
Then I try to pass that to php using $.ajax like below
$.ajax({
url: "../folder/file.php",
type: "POST",
data: {
'customerinfo' : customerinfo,
},
success:function(){
window.location = "file.php";
}
})
In php I try to print the object out
$customerinfo = json_decode($_POST['customerinfo']);
print_r($customerinfo);
or
$customerinfo = $_POST['customerinfo'];
print_r($customerinfo);
I get undefined index customerinfo
I also tried to json.stringify in the javascript before passing it but get same error.
I found a very similar post such as jquery ajax object array php but I couldnt get the code to work either.I think the ajax was able to pass the data through, just I dont know how to decode it on the php side.
I know I could just post it from html submit button, but This is simplified testing for a larger issue where I want to pass object that have many objects inside of it using jquery.
if the javascript side is correct, the question is "how to access objects passed through jquery ajax in PHP."
if not, the question would be "how to pass objects from javascript to php."
success:function(data){ doSomethingWith(data); }