Simple PHP Search

Discussion in 'Programming' started by fordP, Jul 17, 2006.

  1. #1
    Hey I have a search on my site but I want to make another form.

    It has a url like this: http://www.google.com/search?hl=en&q=SEARCHSCRIPT

    where it puts the search term into the URL. Using php how can I tell the form to submit and insert "SEARCHSCRIPT" into the url AND redirect to that page (when "SEARCHSCRIPT" is the term that is entered into the form?)


    I AM NOT going to use this to avoid using GOOGLE's search or anything like that.
     
    fordP, Jul 17, 2006 IP
  2. Boby

    Boby Peon

    Messages:
    207
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can send the keywords directly to the URL via GET through the form:
    <form action="search.php" method="get">
       <input name="q" id="q" value="" />
       <input type="submit" value="Search" />
    </form>
    HTML:
    Or if you want to get the search terms through POST you have them already in your script, and want now to redirect the page:
    header ("Location: http://www.google.com/search?hl=en&q=YOUR-SEARCH-TERMS-HERE");
    exit();
    PHP:
    Hope this helps.
    Boby
     
    Boby, Jul 17, 2006 IP
  3. fordP

    fordP Peon

    Messages:
    548
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It did work in the google example, but the problem is that my URL is actually:

    http://www.mydomain.com/index.php?option=com_sobi&task=search&Itemid=99999999&search_str=EXAMPLESEARCH&search_in=city&sobi_order=name

    I don't know how to make it insert in the middle, and when i put the long string in there it added %20 etc etc ?

    thanks in advance guys@
     
    fordP, Jul 18, 2006 IP
  4. Boby

    Boby Peon

    Messages:
    207
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Then you have to get first the search terms, build the URL with the search terms in the middle and redirect.

    For example consider this:
    $searchTerms = (!empty ($_REQUEST['search']) ? trim (urldecode ($_REQUEST['search'])) : '');
    $url = 'http://www.mydomain.com/index.php?option=com_sobi&task=search&Itemid=99999999&search_str='.urlencode ($searchTerms).'&search_in=city&sobi_order=name';
    header ('Location: '.$url);
    exit();
    PHP:
    %20 etc etc come up because the URL is encoded to prevent erros with whitespace and some more chars.

    Boby
     
    Boby, Jul 18, 2006 IP
  5. fordP

    fordP Peon

    Messages:
    548
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #5
    So do I put that in a php document and then have the form call it up?
     
    fordP, Jul 18, 2006 IP
  6. Boby

    Boby Peon

    Messages:
    207
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The way you want it...with the keywords in the middle of the url, is some sort of double work.

    You first have to create a form (action="little-script.php") that sends the data to the a little php script with the code above. This one will then resend the data to a second script (your url) that will do the search.
     
    Boby, Jul 18, 2006 IP
  7. fordP

    fordP Peon

    Messages:
    548
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #7
    wow I got it thanks a lot
     
    fordP, Jul 18, 2006 IP
  8. Boby

    Boby Peon

    Messages:
    207
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    :) Your welcome!
     
    Boby, Jul 18, 2006 IP