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?
use the super global
$_REQUEST
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.
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/
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)
<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.
Related
I am fairly new to using PHP so bear with me if this is a stupid question
I have a form that comprises a number of radio buttons, the action is set to redirect to the same page and the method is GET.
A click on a radio button gets data from the database. The data is used to redisplay the same page with changed content.
The page URL has PHP arguments in it like the example below
localhost/basesite/mypage.php?itemID=8&name=city&number=9
When I access the page and click on a radio button I get a page with “no arg” because the URL reads
localhost/basesite/mypage.php?number=6
Two of the arguments are missing and that the last one is incorrect.
With no change whatsoever to the code except using ”post’ instead of “get” the whole thing works flawlessly.
I have used
form action= "" method=“get”
form action= “#” method=“get”
and many other actions using $_SERVER["REQUEST_URI”], $_SERVER['QUERY_STRING'] etc and combinations thereof.
Those that worked with POST did not work with GET.
I do not need to use POST as data is not written only retrieved from the database so I have no worry about data being written more than once.
If I have to I will use POST but if the user refreshes or uses the back button then the usual warnings will be issued by the browser.
What am I missing?
you should you use $.get which is a jquery method.
First, you should share your full source code for better understanding your problem. And also you have to use post method to submit a radio button values to get some value from your database. Form data can be submitted using these two methods (get and post). Both are used for the same purpose, but stands apart under some specifications. As in GET method key values are passed in the Url while in POST, the information transfers in a hidden manner.
Sorry folks. It was a badly formed URL due to me not fully understanding how to set a hidden element.
Is it good practice to use form post to send data to other pages versus just html link ($_GET) method.
Say if i have page users.php with all users of the site listed then i want to to page user_details.php that lists details about particular user - i can do it two ways.
Details
or
<form action="user_details.php" method="post">
<input type="hidden" name="user_id" value="742" />
<input type="button" name="nothing" value="User Details" />
</form>
or i could have code load every time that will put $_POST data into $_SESSION then on page user_details.php check user_id in $_SESSION is that even better security practice?
EDIT: This site requires authentication before they can see anything.
There only about 10 pages for admin total so i dont think they need to bookmark it.
For web applications, to me, the basic guideline is:
use GET for cases where you are not going to be modifying data (READ only)
use POST for cases where you are modifying data (WRITE/UPDATE only)
use POST for cases where you need to validate some data in order to return a variable result type (i.e. logins, contact forms, etc. where based on the data being sent, the behavior of the page returned can vary)
For typical web applications, there is also a consideration for whether you want a page/resource to be navigable with the data configuration. In other words, do you want someone to be able to bookmark that page and see that same exact data representation (a data read). If so, use GET.
This also falls nicely in line with REST paradigms where the following HTTP actions are typically supported:
GET -> read specified resources
POST -> create a new data element on data resource
PUT -> update an existing data element on resource
DELETE -> delete an existing data element on resource
One major benefit of using $_GET for this is that it allows your users to bookmark the url, essentially link to it instead of forcing the user to complete a form. If you use $_POST this isn't possible.
Using a querystring for this, however, isn't the most user friendly when it comes to URLs. It would be better to use some sort of pretty url structure like Wordpress does or Stackoverflow does for user pages. This can be done by editing your .htaccess file on your server.
I just setup some pagination for a search, and the search uses POST variables to define what to search for etc. In my URL I can set the pagination offset like this search/OFFSET, and my links in the pagination link there correctly. However, when I click a link all POST variables vanish even if I explicitly set them so I can use them in the next script. I'm using codeigniter and I have GET turned off and really don't want to store these 5-6 values in a session since then it will get all clumsy.
Does clicking a link fully reload the page and delete POST variables?
Thanks
Yes, clicking a link creates a GET request so wouldn't keep any of the POST data. Although it's technically possible to do so with javascript, that's a bad idea.
This is an entirely appropriate use of GET, please read this fuller explanation.
Yes, clicking a link removes all the POST variables.
Do you have access to change your php page that receives the request? You might want to adjust your variables there to accept either GET or POST:
$defaultvalue='';//change this to '' or NULL or whatever you want
$searchQuery = (isset($_POST['s']) ? $_POST['s'] : (isset($_GET['s'])?$_GET['s']: $defaultvalue));
Then your php page will be better equipped to handle either GET or POST
POST data will only be present during the original request (i.e. it does not persist between requests). If you want data to persist, use sessions. However, it is common practice to use GET for search queries and pages.
You could use an incredibly ugly workaround and set a form full of hidden fields to submit when you click a link. I really wouldn't recommended it though.
You should be able to create a form that submits some set of post variables to the action parameter with the get variables. So the form should submit a post request to http://www.somedomain.com/FormSubmit.php?pag=1&sort=asc This would submit the post values of that form along with the get values of the string. If you can change your link to a form button, you should be good to go.
I want to do a search with pagination, but I don't know how to 'store' the data in the $_POST array, should I do it with sessions?
Rolensen
If you are doing a search, you are trying to GET data from the server, and not send data to it -- which means you probably should use GET, and not POST.
Also, it would allow your users to bookmark the result pages (or send those links by e-mail, IM, ...), which is always nice ; and, also, use the back/forward buttons of the browser without getting an alert box, which is nice too ^^
(Oh, and, BTW, it would help solve your problem ;-) )
Yes, you can use sessions or hidden fields and even better GET method in your form.
It is possible to use both GET and POST in form, just add appropriate attribute method to form tag:
<form action="index.php?page=5" method="POST">
So the pager links are submit buttons while rest of the data is stored in hidden fields. But that's not a good way to do this because you cannot pass someone link (on IM for example) to your search results.
But the best way is to store somewhere POST input data (look here: http://www.symfony-project.org/plugins/, when you input your query once, it is stored and remembered so you dont need to fill form multiple times)
i've a search bar from where users can search for videos..
after the search, the user goes on to click and watch a video(from the search result)..when the user hits the browser's back button it displays the following
To display this page, Firefox must
send information that will repeat any
action (such as a search or order
confirmation) that was performed
earlier.
instead of just showing the results..
i just want to display the results of search when the user hits back..is it something related to cache?
thankx..
Preferably, change your search form's method to get, or use the Post/Redirect/Get pattern.
That message is displayed because POST data needs to be resent to re-render the page.
If you don't want that, you can use GET instead of POST for your form method.
ie, you should use something like this :
<form action"yourpage.php" method="get">
...
</form>
instead of :
<form action"yourpage.php" method="post">
...
</form>
This way, the parameters used in the form will be sent in the URL, and not by POST, and users will be able to :
use back with no problem, independantly of the browser's cache
bookmark the result page (which is nice ^^ )
And I should add that, in theory :
POST has to be used when you want to create/modify data
and GET has to be used when you want to... well, get, data.
When searching for something via a search form, your users are not creating nor modifying data : they are asking for data -- so, your form should use the GET method.
To remove this problem, replace the existing top.location.reload() or parent.location.reload(); with top.location.href=top.location.href;.
No, it's not a cache issue. The problem is that you are using POST to submit the search form. There are a couple of solutions.
The better one is change the form to use GET. You can do that by setting its method attribute like
<form method="get" action="search.php">
The other, if you fear that the search query will get way too long you can save the search with some form of id and then redirect to a page that fetches this search id and shows the result. Redirecting can be done with
header('Location: show_search.php?id=' . $searchId);