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:

<html>

<head>

    <script>

        function showData(str)
        {
            if (str=="")
            {
                document.getElementById("ajax-content").innerHTML="";
                return;
            } 

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

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

            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("ajax-content").innerHTML=xmlhttp.responseText;
                }
            }

            xmlhttp.open("GET","showData.php?id="+str,true);

            xmlhttp.send();

        }
        return true;

    </script>

</head>

<body>


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

</body>
</html>

and here is my showData.php file:

<?php

    // Receive variable from URI
    $id=$_GET["id"];

    // 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
    mysql_close($con);

?>

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!

you need to learn JSON

1) in showdat return data in json format
2) take xmlhttp.responseText in some object
3) evaluate json object and copy fields to html fields

http://www.w3schools.com/json/default.asp

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.

jQuery:

$(document).ready(function(){
        $('#go').click(function(){
            var staff_id = $('#staff_id').val();
            if($.isNumeric(staff_id)){
                var data = 'staff_id=' + staff_id;

                $.ajax({
                    url: 'post.php',
                    type: 'POST',
                    data: data,
                    dataType: 'JSON',
                    cache: false,
                    success: function(data){
                        $('#sid').html(data.staff_id).show();
                    }
                }); 
            }
            return false;
        });
    });

PHP Code:

if($_POST['staff_id']){
        $staff_id = $_POST['staff_id'];

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

        echo json_encode($data);

    }

HTML CODE:

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

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

MySQL:

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

 $returns = mysql_fetch_array($get);

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

    echo = json_encode($data);
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.