简体   繁体   中英

get value in textbox from database according to seleceted dropdown item in php

So I've to change text box data according to option selected in dropdown. I want that text box data to be fetched from database.

For eg: If I choose Airmail then current frank value of airmail should be displayed in text box from database.

Here is my code

<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;
}
?>

I want current frank value to be displayed in value_frank feild..and before submit.

add onchange() for select like this,

<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>

then in script write an ajax function to fetch the data from server.

this is the script.

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

in your ajax file get the value from db based on the value selected.and display in your text boxes.

You have to move your database part to this file,

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.
<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

(Create this page. And, remember if you want to change this page name, you have to change the same name in <script> tag too. Both are ralated.)

<?
$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;?>">

first add value_frank id in text box then use given jquery

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM