的index.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 WHERE highway_state = dropdown1显示整个sql表,一旦A部分被修复,是否有关于这样做的任何建议?
答案 0 :(得分: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;
}
答案 1 :(得分: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;
}
可能对你有所帮助。
答案 2 :(得分:0)
显然,问题是我将文件命名为index.html而不是index.php,对不起那些家伙并感谢您的帮助