Search code examples
phpformsinputgetlowercase

Changing form input to lowercase


I'm using a code for a form that redirects the user based on the input of the form. I got it from here:

php - How do I redirect a user based on their form input?

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']);

The problem I'm having is when a user types using CAPS LOCK, it doesn't redirect them.

Is there any way to change the input to lowercase once the user has submitted it which would then allow them to be redirected properly.


Solution

  • You also might url-encode the query, because of characters as "#" or ":", which have a special meaning/function for web-browsers. So this won't break your navigation, if the user has used those characters.

    Reference: Php Doc - urlencode()

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

    combining this with Marc B's answer above, it would result to:

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

    To resolve the original lower-cased user-input on the next pages, just use urldecode():

    Reference: Php Doc - urldecode()

    // original lower-cased query
    $original_query = urldecode($path);