Recover JSON in PHP [closed]

-2

I saw many forums but it still did not work for me

HTML

<div class="form-group col-xs-12 col-sm-5 col-md-6 col-lg-6">
  <label for="banco">Banco</label>
  <select id="banco" type="number" class="form-control" required="" >
    <option value="">Selecione</option>
    <option value='{"sigla" : "bradesco", "banco": "Bradesco"}'>Bradesco</option>
    <option value='{"sigla" : "bb", "bank": "Brasil"}'>Brasil</option>
    <option value='{"sigla" : "cef", "bank": "Caixa Economica Federal"}'>Caixa Econ&ocirc;mica Federal</option>
    <option value='{"sigla" : "itau", "bank": "Itau"}'>Ita&uacute;</option>
    <option value='{"sigla" : "santander_banespa", "bank": "Santander"}'>Santander</option>
  </select>
</div>

Javascript

I have in JavaScript that makes the following submission:

function salvar(){
jQuery('#form').submit(function () {
    var banco       = document.getElementById('banco').value;
     $.ajax({
     type    : 'post',
     dataType: 'json',
     url     : 'function/conta.php',
     beforeSend : carregando,
     data: {                
           'banco'      : JSON.stringify(banco),         
           },
           success: function (data) {
           //alert(data.retorno);
           if (data.retorno == 1) {
           sucesso('Opera&ccedil;&atilde;o realizada com sucesso!');
           }
           else {
           errosend('N&atilde;o foi poss&iacute;vel realizar opera&ccedil;&atilde;o. Verifique se todos os campos est&atilde;o preenchidos ');
          }
        }
      });
    return false; 
 });
}

PHP

In PHP it returns:

if(isset($_POST['banco'])){
    $banco = $_POST['banco'];
}

$bank1 = json_decode($banco);

foreach ($bank1 as $item => $value) {
        echo $value->{'bank'};
    }

But the message you give is:

  

Warning: Invalid argument supplied for foreach () in

Each case is a case, since in another file I use this foreach block it works rs

    
asked by anonymous 26.03.2017 / 05:16

1 answer

1

There are several ways to do what you need, but I'm using your concept.

In Javascript try to send the content of the selected option as it is:

function salvar(){
jQuery('#form').submit(function () {
    var banco       = document.getElementById('banco').value;
     $.ajax({
                type    : 'post',
                dataType: 'json',
                url     : 'function/conta.php',
                beforeSend : carregando,
                data: {

                    'banco'      : banco,

                },
                success: function (data) {
                    //alert(data.retorno);
                    if (data.retorno == 1) {
                        sucesso('Opera&ccedil;&atilde;o realizada com sucesso!');
                    }
                     else {
                        errosend('N&atilde;o foi poss&iacute;vel realizar opera&ccedil;&atilde;o. Verifique se todos os campos est&atilde;o preenchidos ');
                    }
                }
              });
        return false; 
});
}

In PHP decode with json_encode

if(isset($_POST['banco'])){
    $banco = $_POST['banco'];
}

$bank1 = json_decode($banco);

echo $bank1->sigla.'<br />';
echo $bank1->bank;
  

The error that I noticed is that your option is sending a   single object and not an array of objects, so it does not have to be   I get (and will not) from the foreach that just loopes with arrays.   With this you can get the object directly.

NOTE: I noticed that your options has a json value that is called bank instead of bank . Beware of this as it may give future errors in the checks.

    
26.03.2017 / 05:51