tags:

views:

141

answers:

2

I want a simple form with one text box and a submit button. For example: If a user enters "foobar" into the text box and hits enter, they should be redirected to mysite.com/browse/foobar

Does anyone know how I can do this in php? thanks

+4  A: 

Assuming name='q' for the text input

<?php
    if ($_GET['q'] === "foobar") {
        header("Location: http://example.com/browse/foobar");
    }
?>
David Dorward
Sorry but I meant "foobar" to mean any word the user wants to enter
Steven
http://uk3.php.net/urlencode plus a tiny bit of string concatenation and you're there.
David Dorward
sorry but I'm not "there" yet. Can you elaborate? thanks
Steven
+1  A: 

The Form:

<form action="index.php" method="get">
<input type="text" name="q" />
<input type="submit" />
</form>

index.php

header("Location: http://example.com/browse/".$_GET['q']);

Note: this is not good practice but it works.

Samuel
Thanks that's what I'm after. But I'm concerned that it's not "good practice". Is there a better way?
Steven
look at mod_rewrite. that is what most sites use to get "clean" URLs.
Samuel
A better way would be to only allow alphanumeric characters in $_GET['q'] - this can be done with $q = preg_replace("/[^a-zA-Z0-9\s]/", '', $_GET['q']);
Syntax Error