lundi 25 avril 2016

PHP/Laravel: POSTed JSON Object Array not setting keys with empty values

This one was really hard to put in a title. I'm using Laravel 5.1 with PHP 5.6 and a jQuery POST on the front end of this.

Consider the following jquery post:

$.post('/postable-url', {
          email: 'email@example.com',
          items: [
                { id: '', name: 'thing1' },
                { id: '1', name: '' }
            ]
      }, function(data){});

So this posts a json object to the server. Now here's the laravel to consume that.

function postable_url(Request $r){
    $email = $r->input('email');
    $items = $r->input('items');
    foreach($items as $i){
        $tmp = $i['id'];
        $tmp2 = $i['name'];
    }

}

Cool? Except this throws an error, Undefined Index 'id', on the $i['id'] field. This just started happening when we upgraded from PHP 5.5->5.6.2. And the weird part is that it works on our Development machines and our test server, which have the same PHP version. I'm not exactly sure what's going on, but I assume I'm missing some php extension. Can anyone tell me what it is?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire