How to get text box to submit without ? and =

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by caftpx10, Jun 6, 2014.

  1. caftpx10

    caftpx10 Well-Known Member

    Messages:
    1,502
    Likes Received:
    110
    Trophy Points:
    63
    I've updated my users page so that it hides '?user=' from the URL which is perfect, but I have this text box which helps searches for a user on that page.
    The problem is that it adds itself as "?user=USERNAME" with or without something existing in the URL so for example, http://example.com/user/USERNAME would become after submission http://example.com/user/USERNAME?user=USERNAME, and if the GET is empty (http://example.com/user/) then it does http://example.com/user/USERNAME.
    What I want it to do is to do http://example.com/user/USERNAME even after submission of the text box (of course using GET).

    I was thinking of a ? or = detection so it would redirect to the proper URL but that seems pretty bad, so I'm out of ideas.
     
    Last edited: Jun 6, 2014
  2. essellar

    essellar Community Advocate Community Support

    Messages:
    3,295
    Likes Received:
    227
    Trophy Points:
    63
    You can't use a GET query without a query string (?foo=bar&baz=qux); you need to use POST to move the data into the request body. It is possible to move the data into a session and then redirect, but if you want the user's browser's address bar to be emptied of the query string, you would need to do that browser-side. (The browser, when issuing a GET request, is waiting for an answer from http://server.example.com/path/page?foo=bar&baz=qux. If you do an internal redirect, the browser will till show the original URL, including the query string, in the address bar. You need to answer that request, but that answer can be "go to http://server.example.com/path/page", whereupon you revive the submitted values from the session and finally answer the request.) If you are using a form, there's no reason not to use POST as the method.
     
  3. caftpx10

    caftpx10 Well-Known Member

    Messages:
    1,502
    Likes Received:
    110
    Trophy Points:
    63
    So, use POST, sanitize the data, echo the data in a meta redirect in a PHP echo (set to 0 seconds) and then it redirecting to http://example.com/user/USERNAME-WHICH-WAS-SUBMITTED-BY-POST ?
    That seems close enough, I mean I just want it to not have a redirection but hey, can't always get what you want. :p
     
  4. essellar

    essellar Community Advocate Community Support

    Messages:
    3,295
    Likes Received:
    227
    Trophy Points:
    63
    If you use POST, there's no need to redirect -- there will be no query string to eliminate. When you use GET, the submitted data is in the URL; when you use POST, it's in a REQUEST_CONTENT package inside the request.
     
  5. caftpx10

    caftpx10 Well-Known Member

    Messages:
    1,502
    Likes Received:
    110
    Trophy Points:
    63
    Ah, the thing is that this is basically a profile page so using POST isn't really a good idea by itself.
     
  6. essellar

    essellar Community Advocate Community Support

    Messages:
    3,295
    Likes Received:
    227
    Trophy Points:
    63
    There's nothing wrong with using POST to get data from the browser to the server. (Unless you're a fundamentalist REST believer, in which case you either like the query string, since that is the canonical GET method of submitting form data, or you build a URL that doesn't need a query string in JavaScript before requesting the page, and skip form submission altogether.) Either way, a redirect is wrong.
     
  7. caftpx10

    caftpx10 Well-Known Member

    Messages:
    1,502
    Likes Received:
    110
    Trophy Points:
    63
    As I've said above, this is a profile page I'm talking about so using the URL to get to it is the main thing, POST is a different way of submitting and you can't modify this data in the URL unlike GET.


    I do understand that a redirect is wrong, but at the moment, I don't really see another solution apart from that which is like it's a limitation in PHP.
     
  8. leafypiggy

    leafypiggy Manager of Pens and Office Supplies Staff Member

    Messages:
    3,819
    Likes Received:
    163
    Trophy Points:
    63
    You'd want to use Mod_rewrite in your .htaccess to rewrite requests to domain.com/user/username into domain.com/index.php?user=username

    Google Fu! :)
     
  9. essellar

    essellar Community Advocate Community Support

    Messages:
    3,295
    Likes Received:
    227
    Trophy Points:
    63
    The problem isn't at the server end, Neil; it's submitting the search form data. If @caftpx10 wants to avoid JS construction of the URL browser-side, then the GET method needs to be used for the form, which will result in a query string. You'd need to do a double rewrite in the .htaccess: one to force the browser to QS-less version (returning a 301 as a "last rule"), then another internal (no redirect) rule to use the redirect rule result to go to the "real" URL and rebuild the query string. The user will still see the ugly URL until the 301 happens at their browser. It's still missing what GET means when used with data submission (the QS is semantically meaningful).
     
  10. leafypiggy

    leafypiggy Manager of Pens and Office Supplies Staff Member

    Messages:
    3,819
    Likes Received:
    163
    Trophy Points:
    63
    But... he's getting a user profile. The application logic for viewing a profile should be the same. if the proverbial $username exists, then show the profile, otherwise, return 404;
     
  11. caftpx10

    caftpx10 Well-Known Member

    Messages:
    1,502
    Likes Received:
    110
    Trophy Points:
    63
    I'm using the .htaccess code in one of the threads, which was suggested by you.

    Code:
    Rewrite /
    
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)$ /user/?user=$1 [L]
    
    That is perfect for one thing.
    I was thinking of a .htaccess redirect if you use the non-clean URL but that for me failed, I may have gone pass it since the redirect was cached but I didn't have time to redo it.
     

Share This Page