0

索引.html:

<?php
    include_once 'func.inc.php';
    connect();
?>

<!doctype html>
<html>
<head>
    <title>Team DriveSmart</title>
</head>
<body bgcolor="#008933" text="#ffffff">
    <header>
        <h1 align="center"> Team DriveSmart </h1>
    </header>
    <header>
        <h3 align="center"> Highway State </h3>
    </header>
    <section>
        <article>
            <hgroup>
            </hgroup>
            <p align="center">
            <form style="text-align:center" method="post" action="">

                <select name="dropdown1">
                    <?php query1() ?>   
                </select>

            <input type="submit" value="Submit" />
            </form> 
            </p>
        </article>
        <article>
        </article>
    </section>
    <footer>
        <f1 align="center"></f1>
    </footer>



    <header>
        <h3 align="center"> Highway ID </h3>
    </header>
    <section>
        <article>
            <hgroup>
            </hgroup>
            <p align="center">
            <form style="text-align:center" method="post" action="">

                <select name="dropdown2">
                    <?php query2() ?>   
                </select>

            <input type="submit" value="Submit" />
            </form> 
            </p>
        </article>
        <article>
        </article>
    </section>
    <footer>
        <f1 align="center"></f1>
    </footer>

</body>
</html>

然后我的 func.inc.php 看起来像这样:

<?php
    include_once 'db.inc.php';

    function connect()
    {
        mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Error connecting" . mysql_error()) ;
        mysql_select_db(DB_NAME);
    }

    function close()
    {
        mysql_close();
    }

    function query1()
    {

        $myData=mysql_query("SELECT * FROM highways_highway");
        while($record = mysql_fetch_array($myData))
        {
            echo '<option value="' . $record['highway_state'] . '">' . $record['highway_state'] . '</option>' ;
        }
    }

    function query2()
    {
        $myData=mysql_query("SELECT * FROM highways_exit");
        while($record = mysql_fetch_array($myData))
        {
            echo '<option value="' . $record['highway_id'] . '">' . $record['highway_id'] . '</option>' ;
        }
    }


?>

我想做的是我想用数据库中的选项填充这些下拉菜单(例如,在 query1 中,我想用状态填充菜单)。该代码目前似乎显示了一个下拉菜单和一个提交按钮,但下拉菜单是空的,我不知道为什么。

B 部分:一旦用户从下拉菜单中选择,我想根据查询 SELECT * FROM Highways_highway WHEREhighway_state = dropdown1 显示整个 sql 表,一旦 A 部分修复,有什么建议吗?

4

3 回答 3

0

尝试这个

function my_custom_function()
{
// php code here

return $myData;
}

或者在你的情况下

function query2()
    {
     $myHtmlData = '';

        $myData=mysql_query("SELECT * FROM highways_exit");
        while($record = mysql_fetch_array($myData))
        {
            $myHtmlData .= '<option value="' . $record['highway_id'] . '">' . $record['highway_id'] . '</option>' ;
        }

   return $myHtmlData;
    }
于 2013-04-25T06:05:13.700 回答
0

用这个。

在 index.html 文件中,像这样回显您的函数。

<select name="dropdown1">
     <?php echo query1(); ?>   
</select>

<select name="dropdown2">
     <?php echo query2(); ?>   
</select>

并在 func.inc.php 文件中,返回这样的数据。

function query1()
{
    $myselectData = '';

    $myData= mysql_query("SELECT * FROM highways_highway");
    while($record = mysql_fetch_assoc($myData))
    {
        $myselectData .= '<option value="'.$record['highway_state'].'">' . $record['highway_state'] . '</option>';
    }

  return $myselectData; 
}

function query2()
{
    $myselectData1 = '';

    $myData= mysql_query("SELECT * FROM highways_exit");
    while($record = mysql_fetch_assoc($myData))
    {
        $myselectData1 .= '<option value="'.$record['highway_id'].'">' . $record['highway_id'] . '</option>';
    }

 return $myselectData1; 
}

这可能对你有帮助。

于 2013-04-25T06:47:58.377 回答
0

显然,问题是我将文件命名为 index.html 而不是 index.php,对不起那些人,感谢您的帮助

于 2013-04-26T00:52:21.940 回答