How To Make A Php Or Html Select Action, Based On Drop Down List For Corresponding Options?

Discussion in 'PHP' started by ritwick123, Jan 27, 2013.

  1. #1
    I'm onto a project and have been stuck at a simple problem that needs a simple solution where i badly need help. I'm trying to design a form where there will be a drop down list with "google", "yahoo" and "bing" as the options and when the user clicks on the submit button the text entered in the text box will be searched in the corresponding website (like google or yahoo or bing ). Here is the code.

    echo "<form class=\"form\"  METHOD=\"GET\" ACTION=\"http://www.google.com/search\" TARGET=\"_blank\"\">";
    echo "<input type=\"Text\" name=\"q\" size=\"25\" maxlength=\"255\" class=\"Ft\">\n";
     
    echo "</td>\n";
    echo "\n";
    echo "<td valign=\"baseline\" style=\"background-color:#E6EAE9;\">\n";
    echo "<div class=\"styled\"> \n";
    echo "<select>\n";
    echo "  <option value=\"Google\">Google</option>\n";
    echo "  <option value=\"Yahoo\">Yahoo</option>\n";
    echo "  <option value=\"Bing\">Bing</option>\n";
    echo "  \n";
    echo "</select>\n"; 
    PHP:


    What i'm trying to do is that when the user types the text and select the option and then clicks on the submit button it will go to the corrosponding search website google or yahoo or bing. If you have a similar html/php solution to accomplish this please let me know or post the link.
    Please post your answers or sugesstions and i'd be grateful. Thanks.


     
    ritwick123, Jan 27, 2013 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    This is a nice example of what you need: http://www.draac.com/searchpull.html

    BTW next time add HTML code between code tags so

    
    <?php
    // here your code
    ?>
    <p>here your html <span class="something">code</span></p>
    <?php
    // here more code
    ?>
    
    PHP:
    its more readable for everyone and because you closed the tags you don't use PHP to parse the parts between ?> and <?php so the script will be a tiny bit faster!
     
    EricBruggema, Jan 27, 2013 IP
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    Submit to (the "action" part of the form) your own PHP file, not to the search engine directly. In that PHP file, use the value of the select (you'll have to name it and gice the options values) to decide which search engine to use. Then you can find PHP examples for submitting to search engines.

    BTW, just type your PHP code, don't echo it. There's no reason for the additional overhead of using PHP to generate the static HTML.
     
    Rukbat, Jan 28, 2013 IP
  4. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #4
    $searchquery = $_POST['searchquery'];

    $googlesearch= "http://www.google.com/#hl=en&tbo=d&sclient=psy-ab&q=" . $searchquery . "&oq=" . $searchquery . "&gs_l=hp.3..0l4.2152.82251.0.82627.292.271.7.6.6.7.708.50700.11j175j52j12j10j3j1.264.0.les%3B..1.0...1c.1.kcULqNj55cQ&pbx=1&bav=on.2,or.r_gc.r_pw.r_qf.&bvm=bv.41524429,d.eWU&fp=c5167c443cd9f80c&biw=1600&bih=799";

    what ever word or string comes from your input text box and is submitted it will be picked up as $_POST variable. and will be fit into the google search query string.
    That way you're making queries from your site. It will send that query to google but you will have to decide how you want the search results to appear on your page. <DIV> table? <IFrame> etc. using google is pretty easy thoughon its own and most search engines allow you to already have your own google search box on your site. Why not use their already created search boxes from those sites?
     
    ezprint2008, Jan 28, 2013 IP
  5. omgitsfletch

    omgitsfletch Well-Known Member

    Messages:
    1,222
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    145
    #5
    Here's a simple example for you:

    page.html
    <form action="redirect.php" method="post">
      <select name="site">
        <option value="google">Google</option>
        <option value="yahoo">Yahoo</option>
        <option value="bing">Bing</option>
      </select>
      <input type="text" name="term" value="Enter Search Term!" onclick="javascript:if(this.value=='Enter Search Term!')this.value=''" onblur="javascript:if(this.value=='')this.value='Enter Search Term!'" />
      <input type="submit" value="Search!" />
    </form>
    HTML:
    redirect.php
    <?php
     
    $valid_choices = array('google','yahoo','bing');
     
    $site = (isset($_POST['site'])) ? $_POST['site'] : '';
    $term = (isset($_POST['term'])) ? $_POST['term'] : '';
     
    if ( $site && in_array($site, $valid_choices) ) {
      switch ($site) {
        case 'google':
          $url = 'http://www.google.com/#q=' . $term;
          break;
        case 'yahoo':
          $url = 'http://search.yahoo.com/search?p=' . $term;
          break;
        case 'bing':
          $url = 'http://www.bing.com/search?q=' . $term;
          break;
      }
      header('Location: ' . $url);
    } else {
      header('Location: page.html');
    }
     
    ?>
    PHP:
     
    omgitsfletch, Jan 28, 2013 IP
  6. ritwick123

    ritwick123 Well-Known Member

    Messages:
    218
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    130
    #6
    Thanks everyone for adequately answering my question. I've solved it myself using all your help. Thanks to omgitsfletch, ezprint2008, rukbat and ericbrugemma.
     
    ritwick123, Feb 2, 2013 IP