Renee Cribe
Renee Cribe

Reputation: 325

Input text value based on select option value loaded dynamically from sql db

I have a form that has a select field that loads the options dynamically from a db result query. Here is what the code looks like. See the description text input afterwards? I need the code to return the description of the item selected under productID. How do I go about this? Thanks very much for all replies.

    <div class="row-fluid">
    <div class="span3">
        <label>SKU</label>
        <?php  echo '<select name="ITEM" id="user" class="textfield1">';
        while($res= mysql_fetch_assoc($sql))
        {
        echo '<option value="'.$res['productID'].'">';
        echo $res['SKU'] ; 
        echo'</option>';
        }
        echo'</select>';

        ?>
    </div>
</div>
<div class="row-fluid">             
    <div class="span3">
        <label>Description</label>
        <input type="text" name="description" value=""/>
    </div>
</div>

Upvotes: 0

Views: 4428

Answers (3)

Mihai Matei
Mihai Matei

Reputation: 24276

You can do this in 2 ways:

First way is by redirecting the page having a $_GET parameter which will contain the product id:

<div class="row-fluid">
    <div class="span3">
        <label>SKU</label>
        <?php  echo '<select name="ITEM" id="user" class="textfield1" 
                      onchange="document.location=\'my-page.php?pid=\' + this.value">';
        while($res= mysql_fetch_assoc($sql))
        {
          echo '<option value="'.$res['productID'].'"';
          // LATER EDIT
            if(isset($_GET['pid']) && $_GET['pid'] == $res['productID'])
              echo 'selected="selected"';
          // END LATER EDIT
          echo '>';
          echo $res['SKU'] ; 
          echo'</option>';
        }
        echo'</select>';

        ?>
    </div>
</div>
<div class="row-fluid">             
    <div class="span3">
        <label>Description</label>
        <?php
            if(isset($_GET['pid']) && is_numeric($_GET['pid'])) {
                $sql = mysql_query("SELECT description 
                                    FROM products 
                                    WHERE product_id='" . mysql_real_escape_string($_GET['pid']) . "'");
                $row = mysql_fetch_assoc($sql);
            }
        ?>
        <input type="text" name="description" value="<?=$row['description']?>"/>
    </div>
</div>

Second way is to have an ajax call and fill description input dynamically, without refresing the page

// this is the JS code
$(document).ready(function(){
   $('#user').change(function(){
       $.POST("my-ajax-call-page.php",
               {pid: $("#user").val()},
               function(data){
                   $('input[name="description"]').val(data.description);
               }, "json");
   });
});

and your my-ajax-call-page.php should be like this:

<?php
    include("mysql-connection.php");

    $sql = mysql_query("SELECT description 
                        FROM products 
                        WHERE product_id='" . mysql_real_escape_string($_POST['pid']) . "'");
    $row = mysql_fetch_assoc($sql);

    echo json_encode("description" => $row['description']);
?>

You will find many examples and documentation for using jQuery library on jQuery library website

Upvotes: 1

Buksy
Buksy

Reputation: 12228

You haven't shown us your SQL query, but I assume that you have a column named description and you are selecting this column in your query too.

So then, you can use jQuery to insert description of selected item to input

<div class="row-fluid">
<div class="span3">
    <label>SKU</label>
    <?php  echo '<select name="ITEM" id="user" class="textfield1">';

    $js_array = array(); // This variable will contain your Javascript array with descriptions
    while($res= mysql_fetch_assoc($sql))
    {
    echo '<option value="'.$res['productID'].'">';
    echo $res['SKU'] ; 
    echo'</option>';

    // Fill your array with descriptions; ID of item will be the index of array
    $js_array[$res['productID']] = $res['description'];
    }
    echo'</select>';
    ?>
    <script>
    var description;
    <?php
    foreach($js_array as $description => $id)
    {
      echo("description['".$id."'] = '".$description."';\n");
    }
    ?>

    $(document).ready(function(){
      $('#user').change(function(){
        $("#description").val(description[$('#user').val()]);
      })
    });
    </script>
</div>
</div>
<div class="row-fluid">             
    <div class="span3">
        <label>Description</label>
        <input type="text" name="description" id="description" value=""/>
    </div>
</div>

Be sure to not forget to add id attribute to your input type="text"

Upvotes: 0

sachin jat
sachin jat

Reputation: 21

<div class="row-fluid">
    <div class="span3">
        <label>SKU</label>
        <?php  echo '<select name="ITEM" id="user" class="textfield1" onchange="showDesc()">';
        $desHTML = "";
        echo "<option value='0'>Please select</option>"
        while($res= mysql_fetch_assoc($sql))
        {
        echo '<option value="'.$res['productID'].'">';
        echo $res["SKU"] ; 
        echo'</option>';
        $desHTML .="<div class'descBox' id='".$res['productID']."' style='display:none'>".$res['description']."</div>";
        }
        echo'</select>';

        ?>
    </div>
</div>
<div class="row-fluid">             
    <div class="span3">
        <label>Description</label>
        <?php echo $desHTML; ?>
    </div>
</div>

Now create one js function and call on onchange of select box. Js function Hint:

$(".descBox").hide(); $("#"+selectedItemValue).show();

Let me know if you need any help for JS function.

Upvotes: 0

Related Questions