SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    858
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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!
    Convert your dollars into silver coins. www.convert2silver.com

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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:

    PHP Code:
    $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);
        }​"
    ;
    $pageContent .= "</script>"

  3. #3
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    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:
    PHP Code:
    $query urldecode($_GET['q']); 

  4. #4
    SitePoint Member blogaddition's Avatar
    Join Date
    Sep 2012
    Location
    India
    Posts
    23
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    just encode the q varialbe in javascript and then pass it by window.location method.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •