Jump to content

PHP AJAX (could u help me ?)


mrhussein

Recommended Posts

Hello, regarding to this example in your website: 134211760.jpgthis example display data from mysql database\table with parameter (dropdown list),and i do input the choices to this list,what if i want same this but the choices in the (dropdown list) come from database\table as well ? anyone can help me plz? this example containing two files as the following: 1. HTML File:

<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">Peter Griffin</option><option value="2">Lois Griffin</option><option value="3">Glenn Quagmire</option><option value="4">Joseph Swanson</option></select></form><br /><div id="txtHint"><b>Person info will be listed here.</b></div></body></html>

2. PHP File:

<?php$q=$_GET["q"];$con = mysql_connect('localhost', 'peter', 'abc123');if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("ajax_demo", $con);$sql="SELECT * FROM user WHERE id = '".$q."'";$result = mysql_query($sql);echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th><th>Age</th><th>Hometown</th><th>Job</th></tr>";while($row = mysql_fetch_array($result))  {  echo "<tr>";  echo "<td>" . $row['FirstName'] . "</td>";  echo "<td>" . $row['LastName'] . "</td>";  echo "<td>" . $row['Age'] . "</td>";  echo "<td>" . $row['Hometown'] . "</td>";  echo "<td>" . $row['Job'] . "</td>";  echo "</tr>";  }echo "</table>";mysql_close($con);?>

Thank you very much... Regards,

Link to comment
Share on other sites

save the html file with .php extntion first: Then for select box do as follows:<?php $query=mysql_query("select * from table-name where ..."); ?><select name="users" onchange="showUser(this.value)"><option value="">Select a person:</option><?php while($data=mysql_fetch_array($query)){<option value="<?php echo $data['id'] ?>"><?php echo $data['name'] ?></option><?php } ?></select>don't forget to create the connection with database...

Link to comment
Share on other sites

actully i changed the extntion to .php and did the following but the page dont want to apear:

<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","data.php?q="+str,true);xmlhttp.send();}</script></head><body><form><?php   $con = mysql_connect('localhost', 'wordpressuser461', 'r_uw7x.-EJh&');if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("wordpress461", $con);$query=mysql_query("select id from marks"); ?><select name="users" onchange="showUser(this.value)"><option value="">Select a person:</option><?php while($data=mysql_fetch_array($query)){<option value="<?php echo $data['id'] ?>"><?php echo $data['name'] ?></option><?php } ?></select></form><br /><div id="txtHint"><b>Person info will be listed here.</b></div></body></html>

it looks there is some error in the code :(

Link to comment
Share on other sites

Here's the first thing I spotted.

<?php while($data=mysql_fetch_array($query)){<option value="<?php echo $data['id'] ?>"><?php echo $data['name'] ?></option><?php } ?>

You should read the error messages. They're very descriptive and they tell you exactly where the error is occurring so that you can find it easily.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...