ctgScott
ctgScott

Reputation: 231

JSON_decode problem with array posted to CodeIgniter

I am certain I am making a simple mistake... but simply can't find it.

Ultimately I am posting a JSON array from an Android app (that part is working), but for the time being I am simply testing between two PHP pages (1: test PHP page with basic form, and 2: the CodeIgniter final destination) Here is what I have:

At the form page:

<form action="bambooinvoice/index.php/api2/newinvoice/4/0/0" method="post">
    <?php 
        $array = array("items"=>array(
            "taxable"=>1, 
            "quantity"=>1, 
            "amount"=>123.99, 
            "work_description"=>"this is a test"));
        $json = json_encode($array);
    ?>
    <input type="hidden" name=json value=<?php $json ?> />
    <input type="submit" name="btnSendForm" value="Send" />
</form>

This creates (which looks good to me):

{"items":{"taxable":1,"Quantity":1,"amount":123.99,"work_description":"this is a test"}}

On the codeIgniter side, I have:

$input = $this->input->post('json');
$items = json_decode($input, TRUE);

$amount = 0;
foreach ($items as $item) // In case there are multiple 'items'
{
    $taxable = (isset($item['taxable']) && $item['taxable'] == 1) ? 1 : 0;

    $invoice_items = array(
        'quantity' => $item['quantity'],
        'amount' => $item['amount'],
        'work_description' => $item['work_description'],
        'taxable' => $taxable
    );

    $this->_addInvoiceItem($invoice_items); //simply adding contents to DB
}

In the end I receive the error: (i have received numerous errors actually in all my tweaking, but this is the one I can't seem to shake)

Message: Invalid argument supplied for foreach()

Edited - to correct a typo.

Upvotes: 0

Views: 7816

Answers (4)

Elias Rodrigues
Elias Rodrigues

Reputation: 501

You can change and not use json_encode and use serialize in you array.

Like this in your form:

<form action="bambooinvoice/index.php/api2/newinvoice/4/0/0" method="post">
    <?php 
        $array = array("items"=>array(
            "taxable"=>1, 
            "quantity"=>1, 
            "amount"=>123.99, 
            "work_description"=>"this is a test"));
        $json = serialize($array);
    ?>
    <input type="hidden" name=json value="<?php echo $json ?>" />
    <input type="submit" name="btnSendForm" value="Send" />
</form>

This serialize function will create this output:

a:1:{s:5:"items";a:4:{s:7:"taxable";i:1;s:8:"quantity";i:1;s:6:"amount";d:123.9899999999999948840923025272786617279052734375;s:16:"work_description";s:14:"this is a test";}}

Then in the codeigniter side you can do this:

<?php
$input = $this->input->post('json');
$items = unserialize($input);

$amount = 0;
foreach ($items as $item) // In case there are multiple 'items'
{
    $taxable = (isset($item['taxable']) && $item['taxable'] == 1) ? 1 : 0;

    $invoice_items = array(
        'quantity' => $item['quantity'],
        'amount' => $item['amount'],
        'work_description' => $item['work_description'],
        'taxable' => $taxable
    );

    $this->_addInvoiceItem($invoice_items); //simply adding contents to DB
}
?>

but be careful, because when you use a foreach with an array who contain more the 1 level. I hope that this help you.

Upvotes: 0

bschaeffer
bschaeffer

Reputation: 2904

You are using $this->input->post('items') when your form is posting a hidden value named json.

If you var_dump($this->input->post('items')), it should be FALSE or NULL.

Try this in your CI script instead:

$input = $this->input->post('json'); // not 'items'
$items = json_decode($input, TRUE);

// Rest of your code...

That should fix that problem, but you also need to make sure your json data is being sent correctly to begin with! var_dump($_POST) should show you if it's making it to your script in one piece.

Upvotes: 3

icc
icc

Reputation: 301

When you encode an assoc. array as JSON it becomes an object and is no longer an array. Now when you decode your JSON php creates objects of your JSON and not an assoc. array.

Upvotes: 0

azat
azat

Reputation: 3565

Try this
<input type="hidden" name=json value='<?=$json?>' /> Or this
<input type="hidden" name=json value='<?=str_replace('\'', '&#039;', $json)?>' />
See htmlspecialchars

Upvotes: 0

Related Questions