如何根据选择的单选按钮或下拉列表更改 PHP 表单重定向?
这个想法是能够根据选择的单选按钮或下拉选项执行一些搜索查询。 我知道我的 PHP 代码是错误的,这就是为什么我需要你的帮助;-)
我的测试页面是 可在此处获取(点击)。
HTML 代码是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Engine Test</title>
<style type="text/css">
.page {
text-align: center;
}
</style>
</head>
<body class="page">
<form method="get" action="actions.php">
<div class="input">
<input type="text" size="80" name="search_field" />
<button type="submit">OK</button>
</div>
<div class="options">
<label for="google">
<input type="radio" name="search_choices_1" id="google" value="google" />Google:</label>
<select name="google_lang_dropdown">
<option id="google_english" value="google_english">Google English</option>
<option id="google_spanish" value="google_spanish">Google Spanish</option>
<option id="google_german" value="google_german">Google German</option>
<option id="google_french" value="google_french">Google French</option>
</select>
<label for="bing"><input type="radio" name="search_choices_1" id="bing" value="bing" />Bing</label>
<label for="wikipedia"><input type="radio" name="search_choices_1" id="wikipedia" value="wikipedia" />Wikipedia:</label>
<select name="wikipedia_lang_dropdown">
<option id="wikipedia_english" value="wikipedia_english">Wikipedia English</option>
<option id="wikipedia_spanish" value="wikipedia_spanish">Wikipedia Spanish</option>
<option id="wikipedia_german" value="wikipedia_german">Wikipedia German</option>
<option id="wikipedia_french" value="wikipedia_french">Wikipedia French</option>
</select>
<br />
<br />
</div>
</form>
<form method="get" action="actions.php">
<div class="input">
<input type="text" size="80" name="media_field" />
<button type="submit">OK</button>
</div>
<div class="options">
<label for="audio"><input type="radio" name="search_choices_2" id="audio" value="audio" />Audio:</label>
<select name="audio_choice_dropdown">
<option id="deezer" value="deezer">Deezer</option>
<option id="jiwa" value="jiwa">Jiwa</option>
<option id="last.fm" value="last.fm">Last.fm</option>
</select>
<label for="google_images"><input type="radio" name="search_choices_2" id="google_images" value="google_images" />Google Images</label>
<label for="video"><input type="radio" name="search_choices_2" id="video" value="video" />Video:</label>
<select name="video_choice_dropdown">
<option id="youtube" value="youtube">Youtube</option>
<option id="dailymotion" value="dailymotion">Dailymotion</option>
<option id="google_video" value="google_video">Google Video</option>
</select>
</div>
</form>
</body>
</html>
PHP 代码是:
<?php
if (!empty($_REQUEST['search_field']))
{
$url = array(
'google_english'=>'http://www.google.com/#hl=en&q=__keywords__',
'google_spanish'=>'http://www.google.com/#hl=es&q=__keywords__',
'google_german'=>'http://www.google.com/#hl=de&q=__keywords__',
'google_french'=>'http://www.google.com/#hl=fr&q=__keywords__',
'bing'=>'http://www.bing.com/search?q=__keywords__',
'wikipedia_english'=>'http://en.wikipedia.org/wiki/Special:Search?search=__keywords__',
'wikipedia_spanish'=>'http://es.wikipedia.org/wiki/Special:Search?search=__keywords__',
'wikipedia_german'=>'http://de.wikipedia.org/wiki/Special:Search?search=__keywords__',
'wikipedia_french'=>'http://fr.wikipedia.org/wiki/Special:Search?search=__keywords__');
header('Location:'.str_replace('__keywords__',preg_replace('/(\ )+/', '+', trim($_REQUEST['search_field'])),$url[trim($_REQUEST['search_choices_1'])]));
die();
}
else if (!empty($_REQUEST['media_field']))
{
$url = array(
'deezer'=>'http://www.deezer.com/music/result/all/__keywords__',
'jiwa'=>'http://www.jiwa.fm/#search/track/{%22q%22%3A%22__keywords__%22}',
'last.fm'=>'http://www.last.fm/music?q=__keywords__',
'google_images'=>'http://images.google.com/images?&q=__keywords__',
'youtube'=>'http://www.youtube.com/results?search_query=__keywords__',
'dailymotion'=>'http://www.dailymotion.com/relevance/search/__keywords__',
'google_video'=>'http://images.google.com/images?q=__keywords__');
header('Location:'.str_replace('__keywords__',preg_replace('/(\ )+/', '+', trim($_REQUEST['media_field'])),$url[trim($_REQUEST['search_choices_2'])]));
die();
}
else
{
// No search query; redirect to search page
header('Location:index.html');
die();
}
?>
我的旧版本带有单选按钮只能正常工作 在此处测试(单击)。
但我不知道如何使用下拉列表来做到这一点:-S
感谢您的回答。
注意:它应该符合 W3C XHTML Strict 标准,并且在禁用 JavaScript 时工作(因为它已经适用于仅单选按钮的版本)。
The idea is to be able to perform some search query depending on radio button selected or drop-down choice.
I know my PHP code is wrong, that's why I need your help ;-)
My testing page is available here (click).
HTML code is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Engine Test</title>
<style type="text/css">
.page {
text-align: center;
}
</style>
</head>
<body class="page">
<form method="get" action="actions.php">
<div class="input">
<input type="text" size="80" name="search_field" />
<button type="submit">OK</button>
</div>
<div class="options">
<label for="google">
<input type="radio" name="search_choices_1" id="google" value="google" />Google:</label>
<select name="google_lang_dropdown">
<option id="google_english" value="google_english">Google English</option>
<option id="google_spanish" value="google_spanish">Google Spanish</option>
<option id="google_german" value="google_german">Google German</option>
<option id="google_french" value="google_french">Google French</option>
</select>
<label for="bing"><input type="radio" name="search_choices_1" id="bing" value="bing" />Bing</label>
<label for="wikipedia"><input type="radio" name="search_choices_1" id="wikipedia" value="wikipedia" />Wikipedia:</label>
<select name="wikipedia_lang_dropdown">
<option id="wikipedia_english" value="wikipedia_english">Wikipedia English</option>
<option id="wikipedia_spanish" value="wikipedia_spanish">Wikipedia Spanish</option>
<option id="wikipedia_german" value="wikipedia_german">Wikipedia German</option>
<option id="wikipedia_french" value="wikipedia_french">Wikipedia French</option>
</select>
<br />
<br />
</div>
</form>
<form method="get" action="actions.php">
<div class="input">
<input type="text" size="80" name="media_field" />
<button type="submit">OK</button>
</div>
<div class="options">
<label for="audio"><input type="radio" name="search_choices_2" id="audio" value="audio" />Audio:</label>
<select name="audio_choice_dropdown">
<option id="deezer" value="deezer">Deezer</option>
<option id="jiwa" value="jiwa">Jiwa</option>
<option id="last.fm" value="last.fm">Last.fm</option>
</select>
<label for="google_images"><input type="radio" name="search_choices_2" id="google_images" value="google_images" />Google Images</label>
<label for="video"><input type="radio" name="search_choices_2" id="video" value="video" />Video:</label>
<select name="video_choice_dropdown">
<option id="youtube" value="youtube">Youtube</option>
<option id="dailymotion" value="dailymotion">Dailymotion</option>
<option id="google_video" value="google_video">Google Video</option>
</select>
</div>
</form>
</body>
</html>
PHP code is:
<?php
if (!empty($_REQUEST['search_field']))
{
$url = array(
'google_english'=>'http://www.google.com/#hl=en&q=__keywords__',
'google_spanish'=>'http://www.google.com/#hl=es&q=__keywords__',
'google_german'=>'http://www.google.com/#hl=de&q=__keywords__',
'google_french'=>'http://www.google.com/#hl=fr&q=__keywords__',
'bing'=>'http://www.bing.com/search?q=__keywords__',
'wikipedia_english'=>'http://en.wikipedia.org/wiki/Special:Search?search=__keywords__',
'wikipedia_spanish'=>'http://es.wikipedia.org/wiki/Special:Search?search=__keywords__',
'wikipedia_german'=>'http://de.wikipedia.org/wiki/Special:Search?search=__keywords__',
'wikipedia_french'=>'http://fr.wikipedia.org/wiki/Special:Search?search=__keywords__');
header('Location:'.str_replace('__keywords__',preg_replace('/(\ )+/', '+', trim($_REQUEST['search_field'])),$url[trim($_REQUEST['search_choices_1'])]));
die();
}
else if (!empty($_REQUEST['media_field']))
{
$url = array(
'deezer'=>'http://www.deezer.com/music/result/all/__keywords__',
'jiwa'=>'http://www.jiwa.fm/#search/track/{%22q%22%3A%22__keywords__%22}',
'last.fm'=>'http://www.last.fm/music?q=__keywords__',
'google_images'=>'http://images.google.com/images?&q=__keywords__',
'youtube'=>'http://www.youtube.com/results?search_query=__keywords__',
'dailymotion'=>'http://www.dailymotion.com/relevance/search/__keywords__',
'google_video'=>'http://images.google.com/images?q=__keywords__');
header('Location:'.str_replace('__keywords__',preg_replace('/(\ )+/', '+', trim($_REQUEST['media_field'])),$url[trim($_REQUEST['search_choices_2'])]));
die();
}
else
{
// No search query; redirect to search page
header('Location:index.html');
die();
}
?>
My older version with radio buttons only works fine as you could test here (click).
But I can't figure out how to do this with drop-down list :-S
Thanks for your answers.
Note: it should be W3C XHTML Strict compliant and working when JavaScript is disabled (as it is already for the radio button only version).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能对文本输入、单选组和选择选项组使用相同的名称属性。
尝试用不同的名称将它们分开,然后使用多个条件来处理每种情况。
例子:
You can't use the same name attribute for the text input, radio group and the select-option group.
Try separating them with different names and then using multiple conditionals to handle each case.
Example: