Change select field as registry without modifying list

Asked

Viewed 42 times

-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?

  • 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.

  • You want to keep the values and then increase with others?

  • You want your code to do what? What is the expected result ?

  • 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.

1 answer

0

Roger, I had to make some adaptations to the code to suit your need, and I’m still not sure. 'Cause the code is a little fuzzy.

First: that in your select options load the same id id which is wrong, because id must be unique to each object.

Second: in the while of your "json". You are making a loop to store the value in an array to make another loop in js. Since it doesn’t make much sense I’m going to simplify:

$html = '';
while($linha = mysqli_fetch_assoc($resposta_transp)){
    $html .= "<option value='{$linha['COD']}' selected>{$linha['TRANSPORTADORA']}</option>";
}

echo json_encode($html);

Notice that I put the Selected imagining that only one bank value will come.

If you just want it to increase, replace the function val() to append()(if you want the new values at the end) or to prepend (if you want to at the beginning). As I am already bringing the options directly by variable date, just add to select.

$.ajax({
    url: "./pages/listas_transp.php",
    type: "POST",
    dataType: "json",
}).done(function(data){
   $("#transp").append(data);
});
  • Actually, I don’t want to add values.... because the values are already there.. type: - each order record has a carrier value, this value is listed in select. - when I place a request number in the search field, when loading, I need that value to be "Selected" in the select field. Without changing the list, just skip to the correct value.

  • I’ll do a test here, if it works out I edit my answer.

  • If you have any questions just ask me to edit.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.