-2
I saw many forums but still not solved 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ômica Federal</option>
<option value='{"sigla" : "itau", "bank": "Itau"}'>Itaú</option>
<option value='{"sigla" : "santander_banespa", "bank": "Santander"}'>Santander</option>
</select>
</div>
Javascript
I have in Javascript that makes the following sending:
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ção realizada com sucesso!');
}
else {
errosend('Não foi possível realizar operação. Verifique se todos os campos estã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 it gives is:
Warning: Invalid argument supplied for foreach() in
Each case is a case, because in another file I use this block foreach works for
Do a test to see what type of data returns. Put before the
foreach
var_dump($bank); ... put here what is displaying to you.– Alisson Acioli
Are you sure JS is generating JSON with simple quotes? I made a test here and returned with double quotes and thus worked perfectly in the PHP.
– Woss
It generated it here
{'sigla' : 'bb', 'bank': 'Brasil'}' (length=34)
– adventistaam
I tried it in PHP and with single quotes it doesn’t work. When I put double quotes it works as it should. The jQuery/Javascript function is correctly generating JSON ?
– Alisson Acioli
I edited it to see if it is better understood the question
– adventistaam
He spoke in php and not jquery. Eu não posso responder, mais essa é uma boa formas, veja uma api de cotação como exemplo: <?php 
$json_str = file_get_contents("https://economia.awesomeapi.com.br/json/all/USD-BRL,EUR-BRL"); 
$jsonObj = json_decode($json_str);
$usd = $jsonObj->USD;
$eur = $jsonObj->EUR; ? > then you just recover with echo {{ $Eur->Ask }}, this shows the current quotation, now just adapt your need.
– Macedo_Montalvão