2

I have two pages add_admin.php and ajax_admin.php when i select name in dropdown it display FIRSTNAME, MIDDLENAME and LASTNAME in TEXTBOXES.

HERE is My Code:

add_admin.php

<!-- Department -->
<div class="col-xs-12 col-sm-12 col-md-12" >
<div class="form-group">
    <select id="faculty_name" name="faculty_name" class="form-control" onchange='fetch_select(this.value)' required>
        <option selected="selected" disabled="disabled">Please Select Faculty</option>
        <?php
            $query = mysql_query("select * from faculty_details");
            while($row = mysql_fetch_array($query))
            {
        ?>
        <option value="<?php echo $row['FACULTY_ID'];?>"><?php echo $row['FIRSTNAME']." ".$row['MIDDLENAME']." ".$row['LASTNAME'];?></option>
        <?php
            }
        ?>
    </select>
</div>
</div>

<!-- First Name -->
<div class="col-xs-12 col-sm-4 col-md-4">
<div class="form-group">
    <div class="input-group">
    <div class="input-group-addon"><i class="fa fa-fw fa-user"></i></div>
        <input type="text" id="fname" name="fname" class="form-control input-md" placeholder="First Name" value="">
    </div>
</div>
</div>

other text box are there contaisn middlename, lastname

Ajax Code

function fetch_select(val)
{
    $.ajax
    ({
    type: 'post',
    url: 'ajax_admin.php',
    data: 
    {
        get_option:val
    },
    success: function (response) 
    {
        $('#fname').val(response);
    }
});

ajax_admin.php

<?php
    if(isset($_POST['get_option']))
    {
        $state = $_POST['get_option'];
        $find=mysql_query("select * from faculty_details where    FACULTY_ID=$state");
        while($row=mysql_fetch_array($find))
        {
        echo "$row[FIRSTNAME]";
        }
    }
?>
10
  • please some one help me in this query how i get multiple textbox value in PHP using AJAX Commented Dec 31, 2016 at 8:00
  • It's not clear where are you stuck. Assuming your code is working you should change in ajax_admin.php echo "$row[FIRSTNAME]"; to echo "$row['FIRSTNAME']"; to get results displayed. Commented Dec 31, 2016 at 8:07
  • and then change it to print_r(htmlspecialchars($row['FIRSTNAME'])); so you dont get hacked Commented Dec 31, 2016 at 8:09
  • FIRSTNAME is fetching in FIRST TEXTBOX I WANT TO FETCH MIDDLENAME tooo in other textbox Commented Dec 31, 2016 at 8:11
  • So you want to create a multi-value select box? Commented Dec 31, 2016 at 8:12

1 Answer 1

1

Get the result as json from the server so that you can easily use it in jQuery:
ajax-admin.php

if(isset($_POST['get_option']))
{
   $state = $_POST['get_option'];
   $row1=array();
   $find=mysql_query("select firstname,middlename,lastname from faculty_details where  FACULTY_ID=$state");

   while($row=mysql_fetch_array($find))
  {
    $row1[]=$row;
  }
  die(json_encode($row1));
}

Ajax Code

function fetch_val(val) {
    $.ajax({
        url:"ajax-admin.php",
        type:"POST",
        data:{"get_option":val},
        dataType:"JSON",
        success:function(data){
            $('#fname').val((data[0].firstname));
            $('#mname').val((data[0].middlename));
            $('#lname').val((data[0].lastname));

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

7 Comments

sir please explain more about this code while adding comment to each line whereever necessary to me & future readers also, which gives help us.
@KUMAR can tell me about what?
why we use $_POST[] here $state = $_POST['get_option']; not $_GET[];
and why you use die before die(json_encode($row1)); not echo?
@KUMAR Here we using $_POST because request type is POST that we set it in ajax request. we put die insted echo, because we may put above php code in the any php file if we put echo it will execute the below lines that's unnecessary. So we stoping the code executing below line by adding die
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.