Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php not getting JSON object

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!

like image 284
1252748 Avatar asked Aug 22 '26 04:08

1252748


1 Answers

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');
    }
});
like image 50
Henrik Karlsson Avatar answered Aug 23 '26 18:08

Henrik Karlsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!