Jump to content
Sign in to follow this  
mrhussein

PHP AJAX (could u help me ?)

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,

Share this post


Link to post
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...

Share this post


Link to post
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 :(

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

thank u i found it :D and this is the correction :

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

Edited by mrhussein

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...