How to implement urlencode in a HTML form that uses GET?

On my search form I want to allow my users to use the + and - for doing advanced searches. For example, they should be able to use +lavender -burns to return results that contain lavender but not burns. Through my research on google I’ve realized that I need to be using urlencode and urldecode to allow a plus to be used in the URL. I’m a bit confused on where I would apply this to a simple form that uses GET. Do I need some sort of javascript that is executed when the Go button is clicked?

$pageContent .= “<form action=‘search.php’ method=GET>”;
$pageContent .= “<input name=type type=hidden value=‘advanced’ />”;
$pageContent .= “<input name=t type=hidden id=‘t’ value=‘8’ />”;
$pageContent .= “<input name=q type=text size=27 maxlength=35 />”;
$pageContent .= “<input name=action type=hidden value=processForm>”;
$pageContent .= “<input class=‘button medium green’ type=submit value=‘Go’ />”;
$pageContent .= “</form>”;

Thanks!

You would need to do this client side by using a JavaScript function to encode the inputted string from the user, see the below example:

$pageContent .= "<form action='search.php' method='get' onsubmit='encodeInput(this)'>";
$pageContent .= "    <input name='type' type='hidden' value='advanced' />";
$pageContent .= "    <input name='t' type='hidden' id='t' value='8' />";
$pageContent .= "    <input name='q' type='text' size=27 maxlength='35' />";
$pageContent .= "    <input name='action' type='hidden' value='processForm'>";
$pageContent .= "    <input class='button medium green' type=submit value='Go' />";
$pageContent .= "</form>";
$pageContent .= "<script type='text/javascript'>";
$pageContent .= "    function encodeInput(form) {
        // Encode the value of the query
        form.elements['q'].value = encodeURIComponent(form.elements['q'].value);
    }&#8203;";
$pageContent .= "</script>";

Usually, the browser will automatically urlencode form fields when a form is submitted VIA GET. Just make sure you decode it in the script that processes the query.

IE:


$query = urldecode($_GET['q']);

just encode the q varialbe in javascript and then pass it by window.location method.