Challenge for you... search form with POST method and PHP

Discussion in 'PHP' started by drawer, Jun 6, 2010.

  1. #1
    Allright so we are using a PHP environment here (WordPress, if you can't guess below).

    We have a search box, and we need the user's input to "post" to a certain URL.

    <form action="http://example.com/tag/" method="post">
    <input type="text" />
    <input type="image" src="submit.gif" value="" />
    </form>
    Code (markup):
    So using this example form code, how can we get the user's search term, let's say "black poodle" to post to the given URL? In other words, without querying any database, we simply need the form to shoot the user over to this example URL:

    http://example.com/tag/black+poodle/

    At which point, our system/database will organize their query by way of tags.

    Any help would be appreciated. Please note that the receiving page (http://example.com/dogs/) is not setup to deal with GET or POST queries, its simply a virtual path etc.

    Thanks for any advice, I'm no programmer! ;)
     
    drawer, Jun 6, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    <form method="post">
    <input type="text" name="search" />
    <input type="image" src="submit.gif" name="submit" />
    </form>
    
    <?php
    
    if (isset($_POST['submit']) && !empty($_POST['search'])) {
    $term = str_replace(" ", "+", trim(strip_tags($_POST['search'])));
    header("Location: http://example.com/tag/".$term);
    }
    
    ?>
    PHP:
     
    danx10, Jun 6, 2010 IP
  3. drawer

    drawer Peon

    Messages:
    236
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Awesome dan, it worked! Took me 10 minutes to figure out that this code will only work ("isset ... etc") if your "submit" input is actually a "type=submit" and not an image submit input.

    I'm going to create some basic templates to workup for the WordPress community... I think a few hundred people will be pretty excited with this hack. Hopefully its pretty safe and stable? Cheers!

    P.S. what happened to the digital point "give points" thing for thanking people on their profile?
     
    Last edited: Jun 6, 2010
    drawer, Jun 6, 2010 IP