如何根据所选的单选按钮或下拉列表更改PHP表单重定向?

时间:2009-09-15 21:05:20

标签: php redirect drop-down-menu search-engine radio-button

这个想法是能够根据所选的单选按钮或下拉选项执行一些搜索查询。 我知道我的PHP代码是错误的,这就是我需要你帮助的原因; - )

我的测试页面为available here (click)

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();
}
?>

带有单选按钮的旧版本只能正常工作test here (click)

但我无法弄清楚如何使用下拉列表执行此操作:-S

感谢您的回答。

注意:它应该符合W3C XHTML严格规定,并且在禁用JavaScript时工作(因为它仅适用于单选按钮版本)。

1 个答案:

答案 0 :(得分:1)

您不能对文本输入,广播组和选择选项组使用相同的名称属性。

尝试用不同的名称分隔它们,然后使用多个条件来处理每种情况。

示例:

switch ($_REQUEST['radio_group']) 
{
    case 'google':
        ... look for $_REQUEST['google_lang_dropdown'] ... 
    break;
    case 'bing':
        .. do something..
    break;
    case 'wikipedia':
        ... look for $_REQUEST['wikipedia_lang_dropdown'] ... 
    break;

}