这几乎是我的第一个ajax脚本之一,所以请记住这一点,因为我还在学习......
我正在尝试执行以下操作:
通过允许用户执行以下操作来显示某个锦标赛的结果:
我的问题
第一个选择框(运动)完全填充,但其他选择框不会填充...任何我没有收到错误消息......
我的代码
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
}
}
?>
我确定我只是忘了添加一个或类似的东西,我已经盯着这一小时的最佳时间,但我找不到错误。任何帮助将不胜感激
答案 0 :(得分:4)
尝试dataType : 'html'
:
var dataString = {id:id};
$.ajax({
type : 'POST',
url: "get_sport.php",
dataType : 'html',
data: dataString,
cache: false,
success: function(html)
{
$(".tournament").html(html);
}
});
答案 1 :(得分:2)
问题出在while循环mysql_fetch_array($sql)
中的get_sport.php中,参数应为$result
....多么令人尴尬
答案 2 :(得分:2)
通过替换,
尝试此操作$(".tournament").change(function()
到
$(document).on('change','.tournament',function()
和int get_sport.php 替换,
while($row=mysql_fetch_array($sql))
到
while($row=mysql_fetch_array($result))
答案 3 :(得分:0)
我无法在你的代码中找到问题,但你可以做的最好的事情是学习热点来发现这类问题。
你可以做一些事情:
答案 4 :(得分:0)
更改
while($row=mysql_fetch_array($result)){...
在get_sport.php中
$result=mysql_query($sql);
因为您使用了
## make an object attr for the column
attr = ['C1:C3159']
## make all lower case
[x.lower() for x in attr]
## initialize an empty list
categories = []
## loop through attr object and append categories to the "categories" list
for i in attr:
if 'pest' or 'weed' or 'disease' or 'cide' or 'incid' or 'trap'/
or 'virus' or 'IPM' or 'blight' or 'incid' or 'rot' or 'suck' in i:
categories.append("pest management")
elif 'fert' or 'dap' or 'urea' or 'manga' or 'npk' pr 'inm' in i:
categories.append("fertilizer")
elif 'wind' or 'rain' or 'irr' or 'alt' or 'moist' or 'soil' or 'ph'\
or 'drip'or 'environ' or 'ec' in i:
categories.append("environment")
elif 'spac' or 'name' or 'stor' or 'yield' or 'rogu' or 'maint'\
or 'cond' or 'prod' or 'fenc' or 'child' or 'row' or 'prun' or 'hoe'\
or 'weight' or 'prep' or 'plot' or 'pull' or 'topp' in i:
categories.append("operations")
elif 'plant' or 'germin' or 'age' or 'bulk' or 'buds' or 'matur'\
or 'harvest' or 'surviv' or 'health' or 'height' or 'grow' in i:
categories.append("life cycle")
elif 'price' or 'sold' or 'inr' or 'cost' in i:
categories.append("market")
elif 'shed' or 'post' or 'fenc' or 'pond' or 'stor' in i:
categories.append("PPE")
else:
categories.append("uncategorized")