-4

I have a HTML form which has 3 text input fields: id, name, and contact. My DB table has same named columns and the values are id=1, name=mark & contact=1234. Now, if I enter '1' in my html form's id field & press enter then how can the rest of the form's field (like: name, contact) will be automatically filled?

What I've done so far is:

my index.html file(I think its wrong because I didn't use JSON before):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type = javascript>

$("#email").bind("change", function(e){
  $.getJSON("lookup.php?email=" + $("#email").val(),
        function(data){
          $.each(data, function(i,item){
            if (item.field == "first_name") {
              $("#first_name").val(item.value);
            } else if (item.field == "last_name") {
              $("#last_name").val(item.value);
            }
          });
        });
});


</script>>
</head>

<body>
<form>
<input  type="text" name="email" id="email" />
<input type="text" name="first_name" id="first_name" />
<input type="text" name="last_name" id="last_name" />

</form>
</body>
</html>

my lookup.php file:

<?php
//look up the record based on email and get the firstname and lastname
$email=$_GET['$email'];

$con = mysql_connect("localhost", "root", "");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("json", $con); //DB name= json

 mysql_select_db("json"); //DB name= json

  $result = mysql_query("SELECT * FROM json1 
     WHERE email LIKE '$email%'");      //DB table name=json1
if (mysql_num_rows($result) == 1) {
        while ($myrow = mysql_fetch_array($result)) {
//build the JSON array for return
$json = array(array('field' => 'first_name', 
                    'value' => $firstName), 
              array('field' => 'last_name', 
                    'value' => $last_name));
echo json_encode($json );
        }
}
?>

2 Answers 2

1

To fill the form without reloading the page you will have to use Ajax to request the data from the server (database) and then fill it using javascript, i suggest reading about ajax functions with jquery.

if you want for example to fill the id and click a button and the page will reload with the data, use the php post method, a quick example:

<?php
    if($_POST['getdata']){
        $id = $_POST['itemID'];
        /*
        connect to the database and get the data here and return in with an array called $data for example
        */
    }
    if($_POST['savedata']){
        /*
        use this if you want to do another action to the form, update the values for example
        */
    }
?>
<form action='' method='POST'>
    <input name="id" value="<?php echo $data['id'];?>" type="text" />
    <input name="name" value="<?php echo $data['name'];?>" type="text" />
    <input name="contact" value="<?php echo $data['contact'];?>" type="text" />
    <input name="getdata" value="Get Data" type="submit" />
    <input name="savedata" value="Save Data" type="submit" />
</form>

UPDATE:

regarding your update

first of all you better use PDO or MySQLi database instead of MySQL function because mysql_* is no longer maintained so since you are fresh to php learn PDO instead.

to your code, if you are expecting a single row from the database select statement, you should not use while() because its used to loop through the mysql object which has more than one row.

if we assume your table has exactly email, firstname, lastname fetch it directly into an array:

$myrow = mysql_fetch_assoc($result);

then convert that array into json and print it out

echo json_encode($myrow);
exit();
Sign up to request clarification or add additional context in comments.

11 Comments

thanks a lot for your reply :) but, actually i wanted to retrieve back the data without using any submit button. if i use ajax, what approach i need to use? do you have any relevant example please?
thanks for the link.. looks like its used JSON. i tried the code but it didn't work for me.. maybe i did some wrong :(
since you (tried) dont be ashamed to paste your code here again, you will get guided until it works
Ok :) shall i edit the post to put the code? because the comment area is too short to put the code..
|
1

Try using some php MVC framework, like Codeigniter, cakephp, yii.

2 Comments

thanks for reply but i'm newbie at this area so no idea about Codeigniter, cakephp, yii :(
Well, you have to spend some time to learn. Good luck. I voted up to your question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.