0

I want to select a branch code from the option and automatically fill the bank date input from mysql database. I applied ajax and its fetches the data alright but it does not show in the input field unless I check with the inspect element from the browser to see the data was fetched. I want the data appear in the input field. I will be glad you helped. Thanks

The html form

<form method="post" action="">
                <!--=== Table Classes ===-->
                <div class="form-group">
                                    <div class="row">
                                        <div class="col-md-4">
                                            <input type="text" name="pdate" required="required" class="form-control" value="<?php echo date('Y-m-d');?>" readonly=""><span>Login date</span><br>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="branch_code" class=" select2  required form-control" required="required" id="branch_code" >
                                            <option></option>
                                            <?php
                    $tempholder = array();
                    $query = mysql_query("SELECT branch_code FROM users");
                    while($row = mysql_fetch_array($query)) {
                    echo'<option value="'.$row['branch_code'].'" >'.$row['branch_code'].'</option>';
                } 
                    ?>
                                        </select><span>Branch code</span><br>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="text" name="amount" class="form-control" id="w_date" required="required" ><span>Bank date</span><br>
                                        </div>
                                    </div>
                                </div>
                                <hr style="border-color:lightblue;">
                <div class="col-md-12">
                <button type="submit" class="btn btn-default"><i class="fa fa-save"> Save</i></button>
                <button type="reset" class="btn btn-default"><i class="fa fa-retweet" ></i>Reset</button>


                    </div>

                    </form>

The script

<script type="text/javascript">
               $(document).ready(function(){
                   $("#branch_code").change(function(){
                     var branch =$("#branch_code").val();
                     $.ajax({
                    type:"post",
                    url:"auto_branch_customer.php",
                    data:"branch_code="+branch,
                    success:function(data){
                          $("#w_date").html(data);
                    }
                     });
                   });
               });
        </script>

The Php

    <?php
$branchcode = $_POST['branch_code'];
$sql = "SELECT working_date FROM days WHERE branch_code = $branchcode";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) {
echo "{$row['working_date']}";
}
?>

3 Answers 3

1

Change $("#w_date").html(data); to $("#w_date").val(data);

Also make sure your service is returning what you need and nothing else.

Sign up to request clarification or add additional context in comments.

Comments

0
$('#w_date').val(data);

i think this should work

Comments

0

To change the value of an input, use

$(input).val("value")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.