简体   繁体   English

Ajax选择框没有填充

[英]Ajax selectbox not populating

This is pretty much one of my first ajax scripts so please keep that in mind as I am very much still learning... 这几乎是我的第一个ajax脚本之一,所以请记住这一点,因为我还在学习......

Im trying to do the following: 我试着做以下事情:

Display results of a certain tournament by allowing user to do the following: 通过允许用户执行以下操作来显示某个锦标赛的结果:

  1. user selects sport 用户选择运动
  2. now select tournament based on sport_type 现在根据sport_type选择锦标赛
  3. now select round based on tournament and sport_type 现在根据锦标赛和sport_type选择回合

在此输入图像描述

My problem 我的问题

The 1st select box (Sport) populated perfectly yet the other select boxes does not populate...any im getting no error messages... 第一个选择框(运动)完全填充,其他选择框不填充...任何我没有得到错误消息...

My Code 我的守则

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

    }
}
?>

Im sure I just forgot to add a statment or something similar Ive been staring at this for the best part of an hour, yet I can not find the error. 我确定我只是忘了添加一个或类似的东西,我已经盯着这一小时的最佳时间,但我找不到错误。 Any help will be greatly appreciated 任何帮助将不胜感激

Try dataType : 'html' : 试试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);
       } 
     });

问题是在while循环中的get_sport.php mysql_fetch_array($sql)参数应该是$result ....多么尴尬

Try this by replacing, 通过替换来试试这个,

$(".tournament").change(function() 

to

$(document).on('change','.tournament',function()

and int get_sport.php replace, 和int get_sport.php替换,

while($row=mysql_fetch_array($sql))

to

while($row=mysql_fetch_array($result))

I can't find the problem in your code but the best thing you can do its learn hot to spot this kind of problems. 我无法在你的代码中找到问题,但你可以做的最好的事情是学习热点来发现这类问题。

You can do a few things: 你可以做一些事情:

  • Use alerts on Jquery to see if your functions are been called. 在Jquery上使用警报来查看是否已调用您的函数。
  • You can debug the PHP or use some Echo or something like that to see if your php pages are been called, or use a tool as fiddler. 您可以调试PHP或使用一些Echo或类似的东西来查看您的php页面是否被调用,或使用工具作为fiddler。
  • Look at network in Chrome to see what the php return. 看看Chrome中的网络,看看php返回了什么。

Change the 改变

 while($row=mysql_fetch_array($sql)){...

in get_sport.php to 在get_sport.php中

while($row=mysql_fetch_array($result)){...

since you used 因为你用过

$result=mysql_query($sql);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM