-1

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."

2
  • success:function(data){ doSomethingWith(data); } Commented Aug 24, 2016 at 20:11
  • I am not trying to get a response from the php. I actually want to do a redirect. so I just put that in the success. Commented Aug 24, 2016 at 20:18

1 Answer 1

0

from what i see the problem might be in the data. try posting with static data, meaning replace the

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(),
    };

with

var customerinfo = {
        customer_name : 'ddd',
        customer_address: 'ddd',
        customer_city : 'ddd',
        customer_state : 'ddd',
        customer_zip : 'ddd'
    };

also try sending the ajax from the browser console, that way you'll be able to see the data being posted

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.