0

我在将从动态下拉列表中选择的变量传递给 PHP 文件时遇到问题。我希望 PHP 选择 db 表中与变量匹配的所有行。这是到目前为止的代码:

选择.php


<html>
<head>
     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
   <script type="text/javascript">
    $(document).ready(function(){
        $("select#type").attr("disabled","disabled");
        $("select#category").change(function(){
        $("select#type").attr("disabled","disabled");
        $("select#type").html("<option>wait...</option>");            var id = $("select#category option:selected").attr('value');
        $.post("select_type.php", {id:id}, function(data){
            $("select#type").removeAttr("disabled");
            $("select#type").html(data);
        });
    });
    $("form#select_form").submit(function(){
        var cat = $("select#category option:selected").attr('value');
        var type = $("select#type option:selected").attr('value');

        if(cat>0 && type>0)
        {
            var result = $("select#type option:selected").html();
            $("#result").html('your choice: '+result);

            $.ajax({
            type: 'POST',
            url: 'display.php',
            data: {'result': myval},
            });



        }
        else
        {
            $("#result").html("you must choose two options!");
        }
        return false;
    });
});
</script>
</head>
<body>
    <?php include "select.class.php"; ?>
    <form id="select_form">
        Choose a category:<br />
        <select id="category">
            <?php echo $opt->ShowCategory(); ?>
        </select>
    <br /><br />
    Choose a type:<br />
    <select id="type">
         <option value="0">choose...</option>
    </select>
    <br /><br />
    <input type="submit" value="confirm" />
    </form>
    <div id="result"></div>
    <?php include "display.php"; ?>

    <div id="result2"></div>         
</body>
</html>

选择.class.php


<?php
class SelectList
{
protected $conn;

    public function __construct()
    {
        $this->DbConnect();
    }

    protected function DbConnect()
    {
        include "db_config.php";
        $this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect     to the database");
        mysql_select_db($db,$this->conn) OR die("can not select the database $db");
        return TRUE;
    }

    public function ShowCategory()
    {
        $sql = "SELECT * FROM profession";
        $res = mysql_query($sql,$this->conn);
        $category = '<option value="0">choose...</option>';
        while($row = mysql_fetch_array($res))
        {
            $category .= '<option value="' . $row['id_cat'] . '">' . $row['prof_name'] . '</option>';
        }
        return $category;
    }

    public function ShowType()
    {
        $sql = "SELECT * FROM specialties WHERE id_cat=$_POST[id]";
        $res = mysql_query($sql,$this->conn);
        $type = '<option value="0">choose...</option>';
        while($row = mysql_fetch_array($res))
        {
            $type .= '<option value="' . $row['id_type'] . '">' . $row['sp_name'] . '</option>';
        }
        return $type;
    }



}

$opt = new SelectList();
?>

这是我希望将变量传递给的 display.php。该文件将从数据库中选择标准,然后在 select.php 中打印结果。

<?php


class DisplayResults
{
protected $conn;

    public function __construct()
    {
        $this->DbConnect();
    }

    protected function DbConnect()
    {
        include "db_config.php";
        $this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
        mysql_select_db($db,$this->conn) OR die("can not select the database $db");
        return TRUE;
    }

    public function ShowResults()

    {

        $myval = $_POST['result'];
        $sql = "SELECT * FROM specialities WHERE 'myval'=sp_name";
        $res = mysql_query($sql,$this->conn);
           echo "<table border='1'>";
           echo "<tr><th>id</th><th>Code</th></tr>";
        while($row = mysql_fetch_array($res))
        {

            while($row = mysql_fetch_array($result)){
            echo "<tr><td>";
            echo $row['sp_name'];
            echo "</td><td>";
            echo $row['sp_code'];
            echo "</td></tr>";
        }
            echo "</table>";
        //} 
        }
        return $category;
    }

}

$res = new DisplayResults();
?>

我真的很感激任何帮助。如果我可以提供更多详细信息,请告诉我。

db图链接:http: //imgur.com/YZ0SuVw

第一个下拉列表来自专业表,第二个来自专业表。我想做的是显示工作表中与下拉框中选择的专业匹配的所有行。这将需要将下拉列表中的变量(结果)的结果转换为作业表中的 spec_code。不确定如何执行此操作。谢谢!

4

1 回答 1

0

我只想概述以下代码块的一些要点:

  1. 你在哪里定义myval
  2. 数据::{'result': myval}结果不需要任何配额更改为数据:{result: myval}
  3. 为什么需要从选定的选项中获取 HTML?最好发送选项值更改

$("select#type option:selected").html();

$("select#type option:selected").val();

if(cat>0 && type>0)
{
    var result = $("select#type option:selected").html();
    $("#result").html('your choice: '+result);

    $.ajax({
    type: 'POST',
    url: 'display.php',
    data: {'result': myval},
    });
}
于 2013-03-03T20:04:31.680 回答