-1
Ola,
I am making a form for creating/changing an order and have a list of items via <select>
where you take the data via PHP/Mysql. When I fill in an existing order number I can make the value of select change according to what I registered, but it is replacing the first item in the list.See if you understand me:
PHP:
<!-- BUSCA PEDIDO-->
<input type="text" name="busca_nro_pedido" id="busca_nro_pedido" placeholder="Pedido..." class="busca_nro_pedido" tabindex="1"/>
<button type="submit" name="btn_buscar_nro_pedido" onclick="carregaDados()">Buscar</button>
// TRANSPORTADORA
$consulta_transp = "SELECT COD, TRANSPORTADORA FROM transp";
$tabela_transp = array();
$resposta_transp = mysqli_query($conexao, $consulta_transp);
while($linha = mysqli_fetch_assoc($resposta_transp)){
$tabela_transp += array($linha['COD'] => $linha['TRANSPORTADORA']);
}
<!-- TRANSPORTADORA-->
<label for="transp" class="label_titulos">TRANSPORTADORA</label>
<select name="transp" id="transp" class="busca_nro_pedido select transp">
<?php foreach($tabela_transp as $chave => $valor){ ?>
<option value="<?php echo $chave; ?>" id="transp_txt"><?php echo $valor; ?></option>
<?php } ?>
</select>
JSON
$tabela_transp = array();
if(isset($_SESSION["ped_transp"])){
$transp_nome = $_SESSION["ped_transp"];
//$transp_nome = "PLIMOR";
$consulta_transp = "SELECT t.COD, p.TRANSPORTADORA, t.TRANSPORTADORA FROM transp AS t INNER JOIN pedidos AS p ON t.TRANSPORTADORA = '{$transp_nome}' ORDER BY t.TRANSPORTADORA";
}else{
$consulta_transp = "SELECT COD, TRANSPORTADORA FROM transp ORDER BY TRANSPORTADORA";
}
$resposta_transp = mysqli_query($conexao, $consulta_transp);
while($linha = mysqli_fetch_assoc($resposta_transp)){
$tabela_transp = array($linha['COD'] => $linha['TRANSPORTADORA']);
}
echo json_encode($tabela_transp);
JS
$.ajax({
url: "./pages/listas_transp.php",
type: "POST",
dataType: "json",
}).done(function(data){
//console.log(data);
//$("#transp").attr("selected");
$.each(data, function(chave, valor){
$("#transp_txt").text(valor);
$("#transp").val(chave);
});
I believe he is doing this because of the JS that is assigning the values of select... How would I change the select field to the same value I have in the order record without replacing the first value in the list. You got me figured out?
Comes in select the value the user has previously chosen?
– Glenys Mitchell
When the page is empty, this listed the values of select... when I type the order number...select changes to the value you have in the order record...but only when it replaces the first item that was in select before fetching the request.
– Róger
You want to keep the values and then increase with others?
– Glenys Mitchell
You want your code to do what? What is the expected result ?
– Pedro Henrique
I want it to jump to the record contained in the select field, according to the record searched by the order number. And do not replace the values.
– Róger