0

I am trying to have 2 separate divs with one labeled Email and the 2nd div named Number. When a user chooses a person from drop down the retrieved info from PHP + MySQL should be added in a div just how it gets added to the div when you click add at: http://jsfiddle.net/QVUHU/84/

I am stuck because I dont know how to add the retrieved info into 2 divs one for email one for number like the one at js fiddle...

Below is my html + ajax and PHP code

HTML + AJAX

<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML+=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Achau</option>
<option value="2">Ravi</option>
<option value="3">Justin</option>
</select>
</form>
<br />


<div id="txtHint">
<b><table>
<tr>
<th>Email</th>
<th>Number</th>

</tr></b></div>



</body>
</html> 

PHP + MySQL

<?php
$q=$_GET["q"];

$con = mysql_connect("tghbdfg1","Userbhgins","Jpgbhfw3");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("UserLogins", $con);

$sql="SELECT * FROM PersonInfo WHERE id = '".$q."'";

$result = mysql_query($sql);

echo " <br />";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Email'] . "</td>";
  echo "<td>" . $row['PNumber'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?> 
1
  • Your PHP file should return JSON or XML so you can easily separate it into email and number. Also, your current HTML output is not valid because it contains b and br tags. Commented Oct 13, 2011 at 19:08

1 Answer 1

1

HTML:

<th>Email: <div id="email"></div></th>
<th>Number: <div id="number"></div></th>

JavaScript:

document.getElementById["email"].innerHTML = "Hello";
document.getElementById["number"].innerHTML = "World";

EDIT:

No need to return html tags in your respnse. There are better ways to do this(JSON response) but this is simplest solution:

while($row = mysql_fetch_array($result))
  {
  echo $row['Email'] . "|" . $row['PNumber'];
  }

Javascript:

Replace

document.getElementById("txtHint").innerHTML+=xmlhttp.responseText;

With:

var emailPhoneArray = xmlhttp.responseText.split("|");

if(emailPhoneArray != null && emailPhoneArray.length() == 2) {
    document.getElementById["email"].innerHTML = emailPhoneArray[0];
    document.getElementById["number"].innerHTML = emailPhoneArray[1];
}
else {
    alert("Wrong response!");
}
Sign up to request clarification or add additional context in comments.

1 Comment

what you are saying does work but how can I split the email and number?

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.