hey...im trying to display messages stored in a database on a page using ajax.
a user should choose from a drop down list the title of the message and then on selection that message should appear just below.
but i cant seem to do it and i dont know why there is no errors in what ive got so far.
here is the code for the drop down box where the user would select the title.
<form>
<select name="users" onchange="showMessage(this.value)">
<option value="">Select a message:</option>
<?php while($rows = mysql_fetch_array($results))
{?>
<option value="1"><?php echo $rows['heading']; ?></option>
<?php } ?>
</select>
</form>
<br />
<div id="txtHint"><b>Message info will be listed here.</b></div>
the drop down is successfully populated with the correct titles.
here is the javascript in the HEAD of the same page:
<script type="text/javascript">
function showMessage(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","showmessage.php?id="+str,true);
xmlhttp.send();
}
</script>
and here is the php file which is used to display the message upon selection.
<?php
$id=$_GET["id"];
$cust = new customer;
$sql="SELECT * FROM messages WHERE m_id = '".$id."'";
$result = mysql_query($sql);
?>
<table border='1' cellspacing="4">
<?php
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th>Heading</th>";
echo "<th>Message</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['heading'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
?>
</table>
could someone please take a look at this and see whats wrong? ive spent so long on it. if you could tell me how to correct it it would be great