根据从SQL DB动态加载的选择选项值输入文本值 [英] Input text value based on select option value loaded dynamically from sql db

查看:82
本文介绍了根据从SQL DB动态加载的选择选项值输入文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有选择字段的表单,该字段从db结果查询动态加载选项.代码如下所示.看到之后输入的描述文字吗?我需要代码返回在productID下选择的商品的描述.我该怎么办?非常感谢您的所有答复.

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>

推荐答案

您可以通过以下两种方式执行此操作:

You can do this in 2 ways:

第一种方法是通过重定向具有$_GET参数的页面,该参数将包含产品ID:

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>

第二种方法是进行ajax调用并动态填充描述输入,而无需刷新页面

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");
   });
});

和您的my-ajax-call-page.php应该是这样的:

<?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']);
?>

您可以在 jQuery库网站

这篇关于根据从SQL DB动态加载的选择选项值输入文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆