0

i want to get all data to be put in input box where i will select in option box the name of the ingredients that i want to view for example in my data my ingID has a 1,2,3 name has a potato,carrot,beans kilo has a 3,4,5

if i have select the potato the value in input ingID will be 1 and value of input name will be potato and value of input kilo will be 5

is this possible?

<input type="text"  id="ingID" name="ID">
<input type="text"  id="name" name="name">
<input type="text"  id="kilo" name="kilo">


<?php
    include "core/database/connect.php";
    $result = mysql_query("SELECT  ingID , name , kilo FROM ingredients");
    while($row = mysql_fetch_array($result)) {
        echo '<option value = " ' .$row["name"]. ' " >'.$row["name"].'</option>';
    }                   
?>  
1
  • I'm in my phone so I can only suggest: put inside the value of the option the Id of the ingredient and the kilos, ex: value='1:5'. using the onchange jquery function retrieve this value with val(), then the name of the ingredient with text(),you can use the split function to divide the information of the value. Finally you can write the value inside the input with: val(value) Commented Sep 30, 2013 at 9:22

2 Answers 2

1

You can simple do this with jQuery. I set the value attribute of each option to the ID of the record from the DB. But you could also use something like data-id.

I have setup a jsfiddle: http://jsfiddle.net/8Pe9E/

HTML:

<div>ID: <input type="text"  id="ingID" name="ID"></div>
<div>Name: <input type="text"  id="name" name="name"></div>
<div>Kilo: <input type="text"  id="kilo" name="kilo"></div>
<div>Ingredients:
<select name="ingredients">
    <option value = "1" data-kilo="3" >potato</option>
    <option value = "2" data-kilo="4" >carrot</option>
    <option value = "3" data-kilo="5" >beans</option>
</select>
</div>

And the jQuery:

$('select[name="ingredients"]').change(function() {
    var opt = $(this).find('option:selected');
    $('input#ingID').val($(opt).val());
    $('input#name').val($(opt).text());
    $('input#kilo').val($(opt).data('kilo'));
});

The select field is currently static, but you can change this easyily.

Sign up to request clarification or add additional context in comments.

2 Comments

but the value that im putting on input box is on database how can i change the value coming from the row of ingredients table?do i ave to use array?im not good on using array .please help.
@AllanPaulMamangunRosero in your case it should look like: echo '<select name="ingredients">'; while($row = mysql_fetch_array($result)) { echo '<option value="' .$row['ingID']. '" data-kilo="' . $row['kilo'] . '">' . $row['name'] . '</option>'; } echo '</select>';
0

You could also use data attribute for that.

include "core/database/connect.php";
 $result = mysql_query("SELECT  ingID , name , kilo FROM ingredients");
 $select = "<select class='getData'>";
 while($row = mysql_fetch_array($result)) {
            $select .= '<option value = " ' .$row["name"]. ' "  data-kilo="'.$row["kilo"].'" data-name="'.$row["name"].'"  data-ingID="'.$row["ingID"].'">'.$row["name"].'</option>';
        }      
 $select .= "</select>";
 echo $select;

And your jquery code.

$(document).ready(function(){

 $(".getData").bind('click',function(){
       var name = $(this).attr('data-name');
       var kilo = $(this).attr('data-kilo');
       var ingID = $(this).attr('data-ingID');

       $("#ingID").val(ingID);
       $("#name").val(name);
       $("#kilo").val(kilo);

 })
})

1 Comment

where did you get the data-name is coming from my database?is it like row['name'] row['kilo'] row['ingID']? im gonna try thanks for reply

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.