【发布时间】:2011-04-16 07:23:39
【问题描述】:
我有一个连接到我的数据库的 php 脚本,并且有两个下拉菜单。现在我想做的是,根据用户选择,对用户选择运行查询。下拉菜单中的选项是不同的国家/地区。
我想保存此输入并使用该值来运行查询。 IE。如果用户选择 USA 和 Brazil,我将运行一些查询,例如 select * from my database where country == selection 1 and selection 2(巴西和美国)。
我如何从下拉菜单中保存用户选择并使用它来运行这样的查询?我更关心实际设置查询而不是编写查询。
任何帮助将不胜感激!
到目前为止我的代码:
<html>
<head>
<title> Welcome! </title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<Form Name ="form1" Method ="POST" ACTION = "page1.php">
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link)
{
$output = 'Unable to connect to the database server.';
include 'output.html.php';
exit();
}
mysql_select_db('top recipes');
if (!mysql_select_db('top recipes'))
{
$output = 'Unable to locate the joke database.';
include 'output.html.php';
exit();
}
function dropdown( $name, array $options, $selected=null )
{
/*** begin the select ***/
$dropdown = '<select name="'.$name.'" id="'.$name.'">'."\n";
$selected = $selected;
/*** loop over the options ***/
foreach( $options as $key=>$option )
{
/*** assign a selected value ***/
$select = $selected==$key ? ' selected' : null;
/*** add each option to the dropdown ***/
$dropdown .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n";
}
/*** close the select ***/
$dropdown .= '</select>'."\n";
/*** and return the completed dropdown ***/
return $dropdown;
}
function dropdowntwo( $nametwo, array $optionstwo, $selectedtwo=null )
{
/*** begin the select ***/
$dropdowntwo = '<select name="'.$nametwo.'" id="'.$nametwo.'">'."\n";
$selectedtwo = $selectedtwo;
/*** loop over the options ***/
foreach( $optionstwo as $key=>$option )
{
/*** assign a selected value ***/
$select = $selectedtwo==$key ? ' selectedtwo' : null;
/*** add each option to the dropdown ***/
$dropdowntwo .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n";
}
/*** close the select ***/
$dropdowntwo .= '</select>'."\n";
/*** and return the completed dropdown ***/
return $dropdowntwo;
}
?>
<form>
<?php
$name = 'my_dropdown';
$options = array( 'USA', 'Brazil', 'Random' );
$selected = 1;
echo dropdown( $name, $options, $selected );
$nametwo = 'my_dropdowntwo';
$optionstwo = array( 'USA', 'Brazil', 'Random' );
$selectedtwo = 1;
echo dropdowntwo( $nametwo, $optionstwo, $selectedtwo );
?>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Select">
</form>
【问题讨论】:
标签: php database drop-down-menu