php 从下拉菜单中保存用户选择并运行查询
我有一个连接到我的数据库的 php 脚本,并且有两个下拉菜单。现在我想做的是,根据用户选择的内容,对用户选择运行查询。下拉菜单中的选项是不同的国家/地区。
我想保存此输入并使用该值来运行查询。即,如果用户选择美国和巴西,我将运行一些查询,例如从我的数据库中选择 *,其中国家 == 选择 1 和选择 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>
I have a php script that connects to my database and has two drop down menus. Now what I want to do is, based on what the user selection is, run queries on the user selection. The options in the drop down menus are different countries.
I want to save this input and use that value to run a query. I.e. if a user selects USA and Brazil, I will run some query like select * from my database where country == selection 1 and selection 2 (brazil and usa).
How exactly do I save the user selection from a drop down menu and use it to run a query like that? I am more concerned about actually setting up the query than writing the query.
Any help would be appreciated!
My code so far:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您想何时运行查询。如果查询将在正常发送后运行,则仅处理发送的请求。
如果您的应用程序比这个单页面更大,您可能应该阅读一些有关当今流行的 MVC 的内容或使用一些 php 框架,而不是像这样混合代码:)
It depends when you want to run your query. If query will be run after normal sending that just process sent request.
It yours application will be larger then this single page, you should probably read something about todays popular MVC or use some php framework and not mix code like this:)