doushansu9012 2015-04-07 05:54
浏览 38

修改PHP文件使用jQuery $ .post

I'm trying to add a value from a selected in my html into a PHP file using jQuery. I have 2 php file, chainmenu.php and function.php. function.php contain 2 functions to get some data from my database. chainmenu.php is used to show the result of a function from function.php. It requires a variable, that is a value from selected option in my html. I was able to retrieve the value, but my problem is that my $.post function doesn't work. I dont know where is the error, is it in my chainmenu.php or in my function.php.

This is my code

jQuery CODE

  <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 Code

<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>

This explanation a little bit messy, but i hope anyone could help me and give me some good advice.

Thank you.

  • 写回答

2条回答 默认 最新

  • dso407787736 2015-04-07 06:00
    关注

    try like this in chainmenu.php

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

    in function.php replace ShowIt() method like below,

    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;
                }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂