tags:

views:

84

answers:

5

I have a search feature on my site that's POST based. Now, however, I want to be able to provide links that search for certain keywords, but this isn't of course possible because the form doesn't handle GET requests. Is there a way around this?

+3  A: 

use the super global

$_REQUEST
SleepyCod
It contanins $_GET, $_POST and $_COOKIES +1 for me
Eineki
globals are evil. And scope in PHP sucks.
apphacker
Hmm isn't $_GET, $_POST or $_COOKIES somewhat necessary for building a webapplication in PHP :D, I think you're misunderstanding the concept or superglobals in PHP...
SleepyCod
+3  A: 

Set the form's method to GET

<form action="/search" method="GET">

This will work if your search app on the server permits searching via get. Just of note, you should be using GET for search anyway. POST is to make modifications and post data. You're "getting" search results, so use GET.

apphacker
I knew this but didn't want to modify it because I assumed it would take a long time. Turns out it was easy enough and now stuff works.
A: 

You can use javascript to POST the form from a link. An example of how to do that is located here:

http://mentaljetsam.wordpress.com/2008/06/02/using-javascript-to-post-data-between-pages/

ristonj
A: 

I would look at changing your form to operate using GET.

Using GET for the search mechanism is appropriate since GET methods are used for requests that are idempotent. i.e. you can perform them repeatedly without concern for changing state. The semantics of POST is that you're posting data and performing a change (regardless of whether that's really happening in this scenario)

Brian Agnew
A: 
<input type="text" id="searchcat"></input>
<form method="POST">
    ...
    <input type="submit" onclick="this.form.action='/search?cat=' + document.getElementById('searchcat').value"></input>
</form>

Maybe this solution will help? Of course the "searchcat" control seems to be a kind of combobox. And onclick handler better to use as JS-function, not inline...

In fact when you click this submit - browser generates all HTTP-headers, collects the request body from your form data and then sends request with url, containing GET variables in itself. This way you'll have both GET and POST data in your search server-side handler.

Even better to change GET variables in action by handling onChange on your controls. But the example is more long and hard-to-read without IDE.

Jet