here is an example that if i enter id then it will retrieve data from database & print it within form fields. my database has only three column- id, name & number. here is my index.html file:




        function showData(str)
            if (str=="")

            // Code for IE7+, Firefox, Chrome, Opera, Safari
            if (window.XMLHttpRequest)
                xmlhttp=new XMLHttpRequest();

            // Code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

                if (xmlhttp.readyState==4 && xmlhttp.status==200)



        return true;




    <p>Please Enter Your Staff ID:</p>
    <div><input onkeyup="if (event.keyCode == 13) showData(this.value); return false;" /></div>

<div id="ajax-content"></div>


and here is my showData.php file:


    // Receive variable from URI

    // Connect to your database
    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
        die('Could not connect: ' . mysql_error());

    // Select your database
    mysql_select_db("test_01", $con);

    // Select all fields from your table
    $sql="SELECT * FROM staffdetails WHERE id = '".$id."'";

    $result = mysql_query($sql);

    while($row = mysql_fetch_array($result))

        echo "Staff Name:" . "<input type='text' value='" . $row['name'] . "'>";
        echo "</br>";
        echo "Contact No.:" . "<input type='text' value='" . $row['number'] . "'>";

    // Close the connection


now what i'm trying to do is, display the data in a given form field rather than print data with the form field. that means, the name & number input field will be already in the form with the id field. when i'll enter id then it will pull back the data & display it in the given form field. anyone can help please? thanks!

Here is some code to get you started. This is a very basic mockup but if you look at the code and run it, this might point you in the direction you're looking for.


            var staff_id = $('#staff_id').val();
                var data = 'staff_id=' + staff_id;

                    url: 'post.php',
                    type: 'POST',
                    data: data,
                    dataType: 'JSON',
                    cache: false,
                    success: function(data){
            return false;

PHP Code:

        $staff_id = $_POST['staff_id'];

        $data = array('staff_id'=>$staff_id);

        echo json_encode($data);



    <input id="staff_id" name="staff_id" value="0"/>
    <input type="button" id="go" value="GO" />
<div>Your Staff ID: <span id="sid"></span></div>

If you are using mysql, and would like to return the data;


$get = mysql_query("SELECT * FROM staff_members WHERE staff_id={$staff_id} ");

 $returns = mysql_fetch_array($get);

 if(mysql_num_rows($returns) > 0){
    foreach($returns as $return){
        $data[] = $return;

    echo = json_encode($data);