如何基于php中的下拉选择动态获取文本字段中的内容 [英] How to Get Content in text field dynamically, based on dropdown selection in php

查看:49
本文介绍了如何基于php中的下拉选择动态获取文本字段中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有两个字段,一个是package_title,另一个是package_cost.我正在使用while循环在下拉列表中显示package_title.当客户从下拉列表中选择package_title时,我想在另一个文本字段中显示其cost(package_cost).请帮助我.

I have two fields in database, one is package_title and another one is package_cost. I am Displaying package_title in drop down using while loop. When customer selects package_title from dropdown then i want to show its cost(package_cost) in another text field. Please help me.

我的php代码:

<div class="form-select-date">
    <div class="form-elements">
        <label>Select Package</label>
        <div class="form-item">
            <select>
                <option value="">Select</option>
                <?php
                $qry = "select * from `hajj_umrah_package`";
                $rec = mysql_query($qry );
                if( mysql_num_rows($rec) > 0)
                {
                    while($res = mysql_fetch_array($rec))
                    {
                        echo "<option value='".$res['id']."'>".$res['package_title']."</option>";
                    }
                } 
                ?> 
            </select>
        </div>
    </div>
</div>
<div class="form-group">
    <div class="form-elements form-adult">
        <label>Price</label>
        <div class="form-item">
            <input type="text" name="cost"  />
        </div>
    </div>
</div>

推荐答案

您可以使用jquery ajax轻松完成此操作.首先,您必须获取其change事件的下拉列表的值.然后调用下面给出的ajax.

You can do this as simple using jquery ajax. First you have to get the value of dropdown on change event of it. And then call ajax given below.

$('#dropdown_id').change(function(){
var package = $(this).val();
$.ajax({
   type:'POST',
   data:{package:package},
   url:'get_cost.php',
   success:function(data){
       $('#cost').val(data);
   } 

});

});

在您编写上述代码的同一目录中创建一个php文件(get_cost.php).在get_cost.php中添加以下代码:

Create one php file(get_cost.php) in same directory where you had written above code. In get_cost.php add following code :

<?php 
if (isset($_POST['package'])) {
    $qry = "select * from `hajj_umrah_package` where id=" . $_POST['package'];
    $rec = mysql_query($qry);
    if (mysql_num_rows($rec) > 0) {
        while ($res = mysql_fetch_array($rec)) {
            echo $res['package_cost'];
        }
    }
}
die();
?>

这篇关于如何基于php中的下拉选择动态获取文本字段中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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