Ajax selectbox not populating
This is pretty much one of my first ajax scripts, so please keep that in mind as I'm still researching a lot ...
I am trying to do the following:
Display the results of a specific tournament, allowing the user to do the following:
- the user chooses a sport
- now select a tournament based on sport_type
- now select round based on tournament and sport_type
My problem
1st select box (Sport) is filling in fine, but other select fields are not filling ... any im not getting error messages ...
My code
result.php
$(document).ready(function()
{
$(".sport").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "get_sport.php",
data: dataString,
cache: false,
success: function(html)
{
$(".tournament").html(html);
}
});
});
$(".tournament").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "get_round.php",
data: dataString,
cache: false,
success: function(html)
{
$(".round").html(html);
}
});
});
});
</head>
<body>
<center>
<div>
<label>Sport :</label>
<select name="sport" class="sport">
<option selected="selected">--Select Sport--</option>
<?php
var_dump($id);
$sql="SELECT distinct sport_type FROM events";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result))
{
?>
<option value="<?php echo $row['sport_type']; ?>"><?php echo $row['sport_type']; ?></option>
<?php
}
?>
</select>
<label>Tournamet :</label> <select name="tournament" class="tournament">
<option selected="selected">--Select Tournament--</option>
</select>
<label>Round :</label> <select name="round" class="round">
<option selected="selected">--Select Round--</option>
</select>
get_sport.php
include("connect.php");
if($_POST['id'])
{
$id=$_POST['id'];
$sql="SELECT distinct tournament FROM events WHERE sport_type ='$id'";
$result=mysql_query($sql);
?>
<option selected="selected">Select Tournament</option><?php
while($row=mysql_fetch_array($sql)){
?>
<option value="<?php echo $row['tournament'];?>"><?php echo $row['tournament']?></option>
<?php
}
}
get_round.php
include('connect.php');
if($_POST['id'])
{
$id=$_POST['id'];
$sql="SELECT DISTINCT round FROM events WHERE tournament='$id'";
$result=mysql_query($sql);
?>
<option selected="selected">Select Round</option><?php
while($row=mysql_fetch_array($result)){
?>
<option value="<?php echo $row['round'] ?>"><?php echo $row['round'] ?></option>
<?php
}
}
?>
I'm sure I just forgot to add a statute or something similar. I've looked at this for most of an hour, but I can't seem to find the error. Any help would be greatly appreciated
source to share
I can't seem to find the problem in your code, but the best thing you can do to find out what's hot is to spot problems like this.
There are several things you can do:
- Use jQuery warnings to see if your functions have been called.
- You can debug PHP or use some kind of echo or something to see if your php pages have been called or use the tool like a fiddler.
- Look at the web in Chrome to see what php returns.
source to share