根据php中的seleceted下拉项,从数据库中获取文本框中的值

时间:2015-10-10 09:24:03

标签: php

所以我要根据下拉列表中选择的选项更改文本框数据。我希望从数据库中提取该文本框数据。

对于例如:如果我选择Airmail,则航空邮件的当前明确值应显示在数据库的文本框中。

这是我的代码

<div>
    <form action="alertfrank.php" method="POST" class="boxin" id="dropdown">
        <select name="type_post" required id="type_post" >
            <option value="Select Post Type" selected>Select Post Type</option>
            <option value="Airmail" >Airmail</option>
            <option value="Airmail AD">Airmail AD</option>
            <option value="Airmail Speed Post">Airmail Speed Post</option>
            <option value="Speed Post">Speed Post</option>
            <option value="Book Post">Book Post</option>
            <option value="Ordinary Post">Ordinary Post</option>
            <option value="Registered Post">Registered Post</option>
            <option value="Registered AD">Registered AD</option>
            <option value="Registered Parcel">Registered Parcel</option>
            <option value="Parcel">Parcel</option>
            <option value="Courier">Courier</option>
        </select>
        <br/>
        <br/>
        Current Frank Value <?php echo '<input type="number" name="value_frank" value="">'; ?> <br/>
        <br/>
        New Frank Value
        <input type="number" name="new_frank" placeholder="New Frank value"/>
        <br/>
        <br/>
        <input type="submit" name="submit" value="Change Value" id="sub"/>
        <br/>
        <br/>
        <br/>
        <br/>
    </form>
</div>
<?php
if (null !== (filter_input(INPUT_POST, 'submit'))) {
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("disp") or die(mysql_error());
    $type_post = (filter_input(INPUT_POST, 'type_post'));
    $_SESSION['type_post'] = $type_post;
    $q = mysql_query("select value_frank from frank where type_post='" . $_SESSION['type_post'] . "'") or die(mysql_error());
    $value_frank = mysql_fetch_row($q);
    $_SESSION['value_frank'] = $value_frank;
}
?>

我希望当前的frank值显示在value_frank feild ..并在提交之前。

3 个答案:

答案 0 :(得分:1)

为这样的选择添加onchange()

<select name="type_post" required id="type_post" onchange="get_data(this.value)" >
     <option value="Select Post Type" selected>Select Post Type</option>
     <option value="Airmail" >Airmail</option>
     <option value="Airmail AD">Airmail AD</option>
     <option value="Airmail Speed Post">Airmail Speed Post</option>
     <option value="Speed Post">Speed Post</option>
     <option value="Book Post">Book Post</option>
     <option value="Ordinary Post">Ordinary Post</option>
     <option value="Registered Post">Registered Post</option>
     <option value="Registered AD">Registered AD</option>
     <option value="Registered Parcel">Registered Parcel</option>
     <option value="Parcel">Parcel</option>
     <option value="Courier">Courier</option>
 </select>

然后在脚本中 编写一个ajax函数来从服务器获取数据。

这是剧本。

function get_data(value){
    $.ajax({
        url: "nameof ajax file .php",
        type: "POST",
        dataType: "HTML",
        async: false,
        data: {value: value}
        success: function(data) {
           //here we can populate the required fields based on value from database                
        }
     });
 }
你的ajax文件中的

根据所选的值从db获取值。并显示在文本框中。

您必须将数据库部分移动到此文件

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("disp") or die(mysql_error());
$type_post = (filter_input(INPUT_POST, 'type_post'));

$q = mysql_query("select value_frank from frank where type_post='" . $type_post . "'") or die(mysql_error());
$value_frank = mysql_fetch_row($q);
$val_fra = $value_frank;

echo $val_fra;// here no need to use sessions at all.

答案 1 :(得分:1)

<div>
    <form action="alertfrank.php" method="POST" class="boxin" id="dropdown">
        <select name="type_post" required id="type_post" >
            <option value="Select Post Type" selected>Select Post Type</option>
            <option value="Airmail" >Airmail</option>
            <option value="Airmail AD">Airmail AD</option>
            <option value="Airmail Speed Post">Airmail Speed Post</option>
            <option value="Speed Post">Speed Post</option>
            <option value="Book Post">Book Post</option>
            <option value="Ordinary Post">Ordinary Post</option>
            <option value="Registered Post">Registered Post</option>
            <option value="Registered AD">Registered AD</option>
            <option value="Registered Parcel">Registered Parcel</option>
            <option value="Parcel">Parcel</option>
            <option value="Courier">Courier</option>
        </select>
        <br/><br/>
          Current Frank Value
         <span id='ShowValueFrank'><input type="number" id="HideValueFrank" name="value_frank" value=""></span><br/><br/>
          New Frank Value<input type="number" name="new_frank" placeholder="New Frank value"/><br/><br/>
        <input type="submit" name="submit" value="Change Value" id="sub"/><br/><br/><br/><br/>
    </form>
</div>
<script>
    $('#type_post').change(function(){
        var PostType=$('#type_post').val();
        $.ajax({url:"Ajax-ShowPostValue.php?PostType="+PostType,cache:false,success:function(result){
            $('#ShowValueFrank').html(result);
        }});
    });
</script>

Ajax-ShowPostValue.php

(创建此页面。并且,请记住,如果您想更改此页面名称,则必须更改<script>标记中的相同名称。两者都是相关的。)

<?
$Con=mysql_connect("localhost","root","") or die(mysql_error());
$DB=mysql_select_db($Con, "disp") or die(mysql_error());
$PostType=$_GET['PostType'];

$q= mysql_query("select value_frank from frank where type_post='$PostType'");
$RowP=mysql_fetch_array($q);

$ValueFrank=$RowP['value_frank'];
?>

<input type="number" id="HideValueFrank" name="value_frank" value="<?echo $ValueFrank;?>">

答案 2 :(得分:0)

首先在文本框中添加value_frank id,然后使用给定的jquery

$("#type_post").on('change', function(){
    $("#value_frank").val($('#type_post').val());
})