Popular input with data from the same table via dropdown

Asked

Viewed 724 times

2

I have a select named after a product.

I want you to select the product name via dropdown it automatically fills a input code of the respective product. The information is in a single table, in case id, id_produto, nome ,serial, log.

I can pull the dice to the dropdown but I don’t know how to link a input with the id_produto.

I hope I got past my doubt.

<select name="produto" id="produto">
<?php $sql = mysql_query("select * from produto");
    while ($row = mysql_fetch_array($sql)) {
       print '<option value="'.$row['nome'].'">'.$row['nome'].'</option>';
    }
?>
</select><br/>
  • Your code has no JS?

  • has yes, I can even link data from other tables, I just don’t know how to do it pulling from the same where I’ve done the select of "name".

  • Do you want when the select is chosen the ID to fill in an input field? That’s it?

  • I want that when I select the product name, it automatically fills an input with the internal code of the product.

2 answers

3


<input type="hidden" id="inputdesejado">
<select name="produto" id="produto">
<!-- Isso ira evitar que dispare o change quando carregar. -->
<option value="" disable>Escolha um produto:</option>
<?php $sql = mysql_query("select * from produto");
    while ($row = mysql_fetch_array($sql)) {
       print '<option value="'.$row['id_produto'].'">'.$row['nome'].'</option>';
    }
?>
</select><br/>

And make a javascript with the following code:

$("#produto").change(function(){
  $("#inputdesejado").val() = $(this).val();
})'
  • I have to create an input with "#inputdesejado"?

  • @Ronny Amarante replied before me :) Play the example on jsfiddle for me to remove my answer

  • @Thiago, there you give your input ID. Create a <input type="hidden" id="nomedesejado"> that name you pass there in jQuery...

  • @Papacharlie, you can leave, I posted quickly only for him to have notion of the code, its explaining better...

  • I still can’t print the product, the input is blank but I’m beginning to understand the logic, I really appreciate you two.

  • @Thiago, my example shows all necessary elements, just run and model as Ronny Amarante’s answer

  • @Thiago, do you have jQuery in your project? If so, check the appropriate selector identifications.

  • 2

    I did it, thank you very much, you both have no idea how much you’ve helped me!

  • I have another question, when I send the data to the bank, the field product and id_product saves the same information, in case always save the id_product in both fields, how to solve this?

  • @Thiago, ask another question to specify the case better, I’m not able to imagine the situation without seeing the code and other details...

Show 5 more comments

1

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function()
{
    $('#produto').change(function() {
        $('#recebe').val( $( this ).val() );
    });
});
</script>

<select name="produto" id="produto">
    <option value="ID-A">a</option>
    <option value="ID-B">b</option>
    <option value="ID-B">c</option>
</select>

<input id="recebe" />

You can see online here on jsfiddle.

Browser other questions tagged

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