views:

1447

answers:

2

Hi!

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"&gt;
<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&amp;q=__keywords__',
        'google_spanish'=>'http://www.google.com/#hl=es&amp;q=__keywords__',
        'google_german'=>'http://www.google.com/#hl=de&amp;q=__keywords__',
     'google_french'=>'http://www.google.com/#hl=fr&amp;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?&amp;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).

+1  A: 

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:

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;

}
simplemotives
Thanks for your fast answer, I updated HTML Code with new dropdown select names but I didn't achieve to make it work with your PHP code, could please give me some improved example?
Mark
Your <input type="radio" value="" /> are missing value attributes.
simplemotives
Done! (My bad) I've just edited it, but it still doesn't work.Could you please improve your PHP code?
Mark
A: 

To make it work with php go to php tutorials