jQuery函数在回调时不返回任何内容

时间:2016-03-11 04:16:59

标签: javascript php jquery

我有一个jQuery函数,它接受表单选择元素的选定选项并将其发送到php文件,该文件应该返回html以填充第二个select元素。不幸的是,jQuery正在解雇,但返回是空的。有什么想法吗?

表单元素:

if ($result) {

            echo '<label>*Team: <select name="team" class="team" style=\'width: 150; font-size: 16px;\' autocomplete="off" tabindex="1">';
            echo '<option value="">Select</option>';

            while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                    echo '<option value="'.$row['TeamID'].'">'.$row['CaptainLast']."  ".date('m-y',strtotime($row['ArrDate'])).'</option>';
            }

            echo '</select></label>';

} else {
            echo "0 results";
}


?>

<label>*Team Member: <select name="member" class="member" style='width: 150; font-size: 16px;' autocomplete="off" tabindex="2">
<option value="">Select</option>
</select></label>

jQuery:

$(document).ready(function(){
    $(document).on('change','.team', function(){
      var id=$(".team option:selected").val();
      var dataString = 'tmid='+ id;
      console.log(dataString);

      $.ajax({
         type: "POST",
         url: "scripts/memfltpop.php",
         data: dataString,
         dataType: 'html',
         success: function(html){
             $(".member").html(html);
         } 
      });

    });
});

和服务器文件:

<?php
$team = $_POST['tmid'];
echo $team;
$con = mysqli_connect('**********', '****', '******', '*******');

if ($con) {
        $sql = "SELECT MemberAdmin.IndID, MemberAdmin.First, MemberAdmin.Last ". "FROM MemberAdmin ". "LEFT JOIN Members ON MemberAdmin.IndID = Members.IndID ". "WHERE Members.TeamID='" .$team. "'";
        $result = mysqli_query($con,$sql) or die(mysqli_error());

        if ($result) {
            while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $id=$row['IndID'];
                $data=$row['First']." ".$row['Last'];
                echo '<option value="">Select</option>';
                echo '<option value="'.$id.'">'.$data.'</option>';

            }
        } else {
            echo "0 results";
        }
        }
        else {
            echo'Not connected to database';
        }

2 个答案:

答案 0 :(得分:1)

要在html中获取响应,您需要在ajax请求中使用dataType html,如下所示:

 $.ajax({
     type: "POST",
     url: "scripts/memfltpop.php",
     data: dataString,
     dataType : 'html',
     success: function(html){
         $(".member").html(html);
     } 
  });

答案 1 :(得分:0)

哇,找到了。我从没想过这会导致SQL吓坏,但我改变了

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

while($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {

当我更新我的SQL语言时,我错过了这一行。谢谢你们!