0

I want to load data from MySql database to a HTTP form When I type the in the text it should load the name of the person

<html>
<head>
    <script type="text/javascript">
    function FetchUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    var data = JSON.parse(xmlhttp.responseText);
  }
  }
xmlhttp.open("GET","finduser.php?q="+str,true);
xmlhttp.send();
}
this.form.name.value=data.name;
this.form.age.value=data.age;
</script>
</head>
<body>
<form method="post" >
Id<input type="text"  name="id" size="5" />    </br>
Name<input type="text" id="name" name="name"  onclick="Fetchuser(this.form.id.value)" ></br>
Age<input type="text" id="age" name="" size="2" /></br>
</form>
</body>
</html>

The PHP code for that.

<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '125');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);

$sql="SELECT * FROM wow WHERE id = '".$q."'";
$resul = json_encode(mysql_query($sql));
echo $resul;
?> 

My question is how can I get both, name and age, to the text boxes. i tried this but still i cant load data

3
  • possible duplicate of Load mySQL Data Into Form Commented Jun 27, 2012 at 11:47
  • You can simply use json. Commented Jun 27, 2012 at 11:48
  • Don't forget to echo the output of your php code so you can expect a non-empty responseText Commented Jun 27, 2012 at 12:49

3 Answers 3

2

You can use json_encode to return a json with all your relevant fields, and in javascript put each field where it belongs.

$result = json_encode(mysql_query($sql));
echo $result; // don't forget to push the output or responseText is null.

And in the javascript:

var data = JSON.parse(xmlhttp.responseText);

Then you access your data with data.name_of_the_field

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

2 Comments

+1 for suggesting the right way to pass and parse data easily.
Thanks Dexter for pointing out the missing echo. The solution should work in you own example. Just check that the "data" variable has the correct values in javascript.
1
 <?php
 $rs=mysql_fetch_object($result );
 $name=$rs->name;
 $age=$rs->age;
 ?>
  Name<input type="text" name="name"  value="<?php echo $name;?>" />
  Age<input type="text" name="" size="2" value="<?php echo $age;?>"  />

2 Comments

I think he wanted an auto suggest feature, so basically an ajax request is a must, and not this solution.
@Wh1T3h4Ck5 i think he wanted an auto suggest feature.
0

put an id like <input id="name", <input id="age" in your text boxes, so you can assign them via the document.getElementById("name"), document.getElementById("age")

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.