I'm trying to send an hidden input with json array value to php and use it in a conditional mysql select:
My input looks like:
<input type="hidden" name="n_processo2" id="n_processo2" value="["203","1430"]">
My Ajax:
$('input[name=ano]').on('change', function() {
var fabricante = $('select[name=fabricante]').val();
var seguimento = $('select[name=seguimento]').val();
var veiculo = $('select[name=veiculo]').val();
var motor = $('input[name=motor]').val();
var ano = $(this).val();
$.ajax({
method: 'post',
url: 'combo.php',
data:{opcao: 'ano', fabricante: fabricante, seguimento: seguimento, veiculo: veiculo, motor: motor, ano: ano},
dataType: 'json',
beforeSend: function(){
$('#status').html('Aguarde, buscando...');
$('div#conteudo').html('');
},
success: function(data){
$('input[name=n_processo2').val(data.processo2);
var fabricante = $('select[name=fabricante]').val();
var seguimento = $('select[name=seguimento]').val();
var veiculo = $('select[name=veiculo]').val();
var motor = $('input[name=motor]').val();
var ano = $('input[name=ano]').val();
var n_processo2 = $('input[name=n_processo2]').val();
$.ajax({
method: 'post',
url: 'combo.php',
data:{opcao: 'ano', fabricante: fabricante, seguimento: seguimento, veiculo: veiculo, motor: motor, ano: ano, n_processo2: JSON.stringify(n_processo2)},
dataType: 'json',
beforeSend: function(){
$('#status').html('Aguarde, buscando...');
$('div#conteudo').html('');
},
success: function(data){
console.log("success:",data);
$('#status').html('');
$('div#conteudo').html(data.conteudo);
}
})
}
});
$("input[name=ano]").keydown(function (e){
if(e.keyCode == 13){
$("input[name=ano]").blur();
}
});
});
And finally my PHP/Mysql code:
$jsonData = array();
$jsonData[] = $automovel->n_processo;
$data['processo2'] .= json_encode($jsonData);
$ar2 = json_decode($processo2, true);
$where2 = array();
foreach ($ar2 as $condition2) {
$value2 = reset($condition2);
$column2 = key($condition2);
$where2[] = $value2;
}
$whereString2 = implode(' AND n_processo IN ', $where2);
$pegaSonda = $pdo->prepare("SELECT * FROM sonda WHERE fabricante = '$fabricante' AND modelo = '$veiculo' $whereString2");
$pegaSonda->execute();
The problem is when i get the results, looks like my php is not receiving the correct data, i can't find why. Can please someone help me please?