Jump to content

PHP SQL Ajax w3sachool exemple help please


petitesouris

Recommended Posts

Hello I am a real newbie and I tried to use the exemple on the w3schools forum but it does not do what I want so if someone could help me and point me out in the right direction I would be so grateful

 

here is my thing

 

I have a sql database with a schedule

 

I want to be able to build a drop down menue user can select a campus from that sql table and return all the data for that selected campus

 

the most magic option would be for them to be able to do multiple select so they could select the campus

 

AT the moment I am only working with one select option but it really does not work :-)

 

here is the php

 

<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','peter','1234','frozzie_orientation');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"orientation_schedule");
$sql="SELECT * FROM orientation_schedule WHERE FID = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>location</th>
<th>title</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
here is the html
<!DOCTYPE html>
<html>
<head>
<script>
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="orientation_schedule" onchange="showUser(this.value)">
<option value="">Select a campus:</option>
<option value="1">Lismore</option>
<option value="2">Coffs Harbour</option>
<option value="3">Gold Coast</option>
</select>
</form>
<br>
<div id="txtHint"><b>Your Campus will be listed here.</b></div>
</body>
</html>
Thank you very much

 

Link to comment
Share on other sites

What happens when you run that code? Are you checking your browser's developer console for error messages?

 

One problem is that your getuser.php page is returning a complete HTML document, that's not correct. You're sending an ajax request for an HTML fragment that you put inside a div on the other page, and a complete HTML document doesn't go inside a div. That page should only return the HTML fragment that you want in the div. Any CSS for that HTML should go in the parent page.

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...