3

For some reason my php can't read the json object javascript has created.

Here is is printed to the console.

{
    "product": {
        "shipsTo": "Please choose …",
        "accountExec": "Please choose …",
        "product": "Please choose …",
        "productSize": "Please choose …",
        "qty": "",
        "comments": ""
    },
    "billing": {
        "billingName": "",
        "billingAttn": "",
        "billingAddress1": "",
        "billingAddress2": "",
        "billingCity": "",
        "billingState": "",
        "billingZip": "",
        "billingPhone": "",
        "billingFax": "",
        "billingEmail": "[email protected]"
    }
}

The only thing I have filled in is the billingEmail.

I get these values by collecting about 15 fields from a form and putting them in a javascript object

$(document).ready(function(){
    $('#CBS_submit').on("click", function(){

    //validate


    //collect information
    var orderInfo = {};
    orderInfo.product = {};
    orderInfo.billing = {};
    orderInfo.product.shipsTo     = $('#CBS_ship_to_select option:selected').val();
    orderInfo.product.accountExec = $('#CBS_account_execs_select option:selected').val();
    orderInfo.product.product     = $('#CBS_product_select option:selected').val();
    orderInfo.product.productSize = $('#CBS_product_size_select option:selected').val();
    orderInfo.product.qty         = $('#CBS_qty').val();
    orderInfo.product.comments    = $('#CBS_comments').val();
    //capture textarea information if other is selected for
    //shipsTo, product, productSize
    if ( get_selected_id ('CBS_ship_to_select') == 4 ) {
        orderInfo.product.shipsTo = $('#other_ship_to_text').val();
    }
    if ( get_selected_id ('CBS_product_select') == 4 ) {
        orderInfo.product.product = $('#other_product_text').val();
    }
    if ( get_selected_id ('CBS_product_size_select') == 4 ) {
        orderInfo.product.productSize = $('#other_product_size_text').val();
    }


    orderInfo.billing.billingName = $('#CBS_billing_name').val();
    orderInfo.billing.billingAttn = $('#CBS_billing_attn').val();
    orderInfo.billing.billingAddress1 = $('#CBS_billing_address1').val();
    orderInfo.billing.billingAddress2 = $('#CBS_billing_address2').val();
    orderInfo.billing.billingCity = $('#CBS_billing_city').val();
    orderInfo.billing.billingState = $('#CBS_billing_state').val();
    orderInfo.billing.billingZip = $('#CBS_billing_zip').val();
    orderInfo.billing.billingPhone = $('#CBS_billing_phone').val();
    orderInfo.billing.billingFax = $('#CBS_billing_fax').val();
    orderInfo.billing.billingEmail = $('#CBS_billing_email').val(); 


    var orderInfoJSON = JSON.stringify(orderInfo);
    console.log(orderInfoJSON);
        $.ajax({
           type: "POST",
           url: "queries/submit_order.php",
           data: "order_info=" + orderInfoJSON,
           dataType: "json",
           success: function (data) {
           console.log('test');


            }
        });
    });

});

then request it on the php page and start putting it into html

<?php
$order_info = $_REQUEST['order_info'];

$order_info = json_decode($order_info, TRUE);
$msg = <<<EOD

<table style='width:600px; font-family:\"Helvetica\",\"Arial\", sans-serif; margin:15px' border="0">

accessing the variables like

{$order_info['product']['product']}

But for some reason, I get nothing submitted. Even though when I copy/paste the object

$order_info = '{"product":{"shipsTo":"Please choose …","accountExec":"Please choose …","product":"Please choose …","productSize":"Please choose …","qty":"","comments":""},"billing":{"billingName":"","billingAttn":"","billingAddress1":"","billingAddress2":"","billingCity":"","billingState":"","billingZip":"","billingPhone":"","billingFax":"","billingEmail":"[email protected]"}}'; it works

what am I doing wrong? Thanks a million!

8
  • 1
    what does a var_dump or print_r of $_REQUEST show? Or a var_dump of $order_info before and after the json_decode? Commented Aug 29, 2012 at 19:54
  • before: {"product":{"shipsTo":"adsf","accountExec":"Greg Coyne","product":"4","productSize":"e","qty":"4","comments":"asdfasdf"},"billing":{"billingName":"adfouh","billingAttn":"aoidf","billingAddress1":"iadufgh","billingAddress2":"","billingCity":"aoidsf","billingState":"","billingZip":"23042","billingPhone":"adsf","billingFax":"asdf","billingEmail":"adsf"}} Commented Aug 29, 2012 at 20:00
  • after json_decode: {"product":{"shipsTo":"adsf","accountExec":"Greg Coyne","product":"4","productSize":"e","qty":"4","comments":"asdfasdf"},"billing":{"billingName":"adfouh","billingAttn":"aoidf","billingAddress1":"iadufgh","billingAddress2":"","billingCity":"aoidsf","billingState":"","billingZip":"23042","billingPhone":"adsf","billingFax":"asdf","billingEmail":"adsf"}} Commented Aug 29, 2012 at 20:01
  • um, that's a print_r after a json_decode? You should be getting an array, not a string . . . Commented Aug 29, 2012 at 20:04
  • @ernie...yeah i know... also echo var_dump($_REQUEST['order_inaasdfadsfdsffo']); yields the same thing. i think the file isn't getting uploaded to the server properly.. Commented Aug 29, 2012 at 20:05

1 Answer 1

1

Sending it as a string might mess stuff up if it contains special characters. Try using this method instead:

$.ajax({
    type: "POST",
    url: "queries/submit_order.php",
    data: {order_info: orderInfoJSON},
    dataType: "json",
    success: function (data) {
        console.log('test');
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

How would I $_REQUEST that on the other end?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.