2015-04-07 89 views
0

我想从我的html中选择的值添加到使用jQuery的PHP文件中。我有2个PHP文件,chainmenu.php和function.php。 function.php包含2个函数来从我的数据库中获取一些数据。 chainmenu.php用于显示函数function.php的结果。它需要一个变量,这是我html中选定选项的值。我能够检索值,但我的问题是,我的$ .post函数不起作用。我不知道错误在哪里,是在我的chainmenu.php还是在我的function.php中。

这是我的代码

jQuery代码

<script type="text/javascript"> 
     $(document).ready(function() { 
      $("select#trafo").attr("disabled","disabled"); 
      $("select#gi").change(function(){ 
       $("select#trafo").attr("disabled","disabled"); 
       $("select#trafo").html("<option>wait...</option>"); 
       var id = $("select#gi option:selected").attr('value'); 
       $("select#trafo").html("<Option>"+id+"</Option>"); 
       $.post("chainmenu.php", {id:id}, function(data){ 
        $("select#trafo").removeAttr("disabled"); 
        $("select#trafo").html(data); 
       }); 
      }); 
     }); 
     </script> 

Function.php

<?php class SelectList 
    { 

//$this->conn is working fine here 

    public function ShowGI() 
      { 
       $sql = "SELECT * FROM gi"; 
       $res = mysqli_query($this->conn,$sql); 
       if(mysqli_num_rows($res)>=1){ 
        $category = '<option value="0">Pilih GI</option>'; 
        while($row = mysqli_fetch_array($res)) 
        { 
         $category .= '<option value="' . $row['idgi'] . '">' . $row['namegi'] . '</option>'; 
        } 
       } 
       return $category; 
      } 

      public function ShowIt() 
      { 
       $sql = "SELECT * FROM It WHERE idgi=$_POST[id]"; 
       $res = mysql_query($sql,$this->conn); 
       $type = '<option value="0">Choose/option>'; 
       while($row = mysql_fetch_array($res)) 
       { 
        $type .= '<option value="' . $row['idIt'] . '">' . $row['name'] . '</option>'; 
       } 
       return $type; 
      } 
       } 

    $opt = new SelectList(); 
    ?> 

chainmenu.php

<?php include "/opsi.class.php"; 
echo $opt->ShowIt(); ?> 

HTML代码

<head> 
<!-- the script here --> 
</head> 
<body> 
<select id=gi> 
<option value="0"> Select </option> 
</select> 
<select id=It> 
<!-- chainmenu.php result should be here --> 
</select> 

</body> 

这个解释有点杂乱,但我希望任何人都可以帮助我,并给我一些很好的建议。

谢谢。

+0

尝试使用Chrome并使用开发工具来检查发生了什么。打开开发窗口,然后点击网络选项卡,清除所有内容,然后点击提交。回到你的开发标签并查看响应。另请看控制台选项卡。这应该给你一些提示。 – MrTechie

+0

在你正在传递chainmenu.php,但在function.php中使用$ _POST ['id']! –

回答

0

尝试像这样在chainmenu.php

<?php include "/opsi.class.php"; 
echo $opt->ShowIt($_POST['id']); ?> 

在function.php取代ShowIt()方法如下图所示,

public function ShowIt($id) 
      { 
       $sql = "SELECT * FROM It WHERE idgi=$id"; 
       $res = mysql_query($sql,$this->conn); 
       $type = '<option value="0">Choose/option>'; 
       while($row = mysql_fetch_array($res)) 
       { 
        $type .= '<option value="' . $row['idIt'] . '">' . $row['name'] . '</option>'; 
       } 
       return $type; 
      } 
+0

感谢您的建议。但它仍然无法正常工作。 – anjaryes

+0

在jquery中alert(id)是什么来的? –

+0

chainmenu.php中的echo var_dump($ _ POST)给出了什么? –

0

有几个错别字ShowIt()用于如$type = '<option value="0">Choose/option>';功能标签没有正确关闭。 在jQuery代码中,您正在检索值并添加html以选择具有id trafo的标记。而在html代码中,select的id是It。

<select id=It> 
<!-- chainmenu.php result should be here --> 
</select> 
+0

是的,我承认它。我会编辑它。 当我修复这些语法错误后,我发现主要问题是$ .post命令。我的chainmenu.php无法从脚本中获取“id”变量.. – anjaryes