Pagination and $_POST - php

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)

Related

Can't redirect to same page when using GET in a form

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.

Other ways to return data other than string $_GET string parameters?

So im asking what options do I have other than redirecting GET variables when Im trying to repopulate a form?
Im thinking I can create session variable and use that also. Am I correct that these are the ONLY 2 ways?
What I have now is:
if ( count($m) > 0 ) {
// there is an error in fields filled out so we are sending user back to form.
$_SESSION["myarray"] = $m;
header("location: ./edit.php?datefield=".$datefield."&text=".$entry."&flag=".$flag);
}
but this reveals my variable to the user. I want to avoid this.
UPDATE
I re-worded my question and the code to be clearer.
I also like found that the solution for me was to use an include('edit-error.php'). Which is basically the original edit.php with everything stripped out but the code needed to generate the form, and I populated edit-error.php with the needed variables. I never had this in my tool box before so I am grateful to the user who suggested it.
One way that you can consider for hide information (not totally, but more stronger than GET) of user is create a JSON with your infos and send a POST request to your page.
Personally, I still preferring sessions, but there are other ways.
The solution I used was provided my user pala_ , so thanks pala_ I up voted your comment.
I used include('edit-error.php'). Which is basically the original edit.php with everything stripped out but the code needed to generate the form, and I populated edit-error.php's form with the needed variables. I never had this in my tool box before so I am grateful to the pala_ who suggested it.
This allows me to conduct server side validation and redisplay the form again with user input, so they can resubmit. I was using header(Location) with paramaters to submit as GET and I didnt want that. Using include() is a perfect solution for me.

Pass data to other website search

Is there a way to search on www.qantas.com.au or other sites that don't use GET method, from my own form?
I mean something like this : http://site.com/search.php?data=myData
I don't want to return result on my webpage, I just want to send data with a href
Depending on the website, many forms may contain CSRF tokens intended to prevent this behavior (imagine if a third party site could 'submit' a form for you to another site, perhaps to trans fer money or write embarassing posts)
That said, for sites that don't implement this feature, it should be possible just to copy their form (from <form> to </form>) including the action="/their/website/controller" and method="POST" (if you want it posted)
If you want to design your own form that submits the same data, just make sure the input fields have a name="blah" the same as the form data value that you want to submit
AJAX is also an option if you dont want to use a form. (see http://api.jquery.com/jQuery.post/)

POST variables disappearing when a link is clicked

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.

PHP: Pass non-form variables between pages?

I have a page. The user submits the page and sends it to a PHP results page. It works fine. Now, I want the results page to link to another page, and for the elements on that page to depend on what was on the results page. I know how to pass form variables to another page, but I don't know anything about passing non-form variables.
From my searching on the web, my best guess is that I should be passing by URL. Is this correct? If so, a possible problem: the page I want the results page to pass to will have a form, and the user will go to yet another results page by clicking submit (the form data will be sent by POST). Can I send the non-form data (the old results page variable) along with the form data, if the user is going to the other page using POST?
I strongly suggest using sessions. It's not that hard to learn, php makes it VERY easy using http://php.net/session_start and the $_SESSION variable.
Advantage is that you will not have to submit a form on every click, and no information will be displayed in plain text in the URL.
There are several options. However, the easiest may be to simply pass the data on using hidden input fields.
Other options would be using the session, storing to a database between forms, or some combination therein.
If you are going to use POST to go to the next page the most simple option is to include the data you want to send along using an input type="hidden" element in your form.
You might consider using a session to pass the data along.
You can embed the non-form data into the second form as hidden fields. To make sure that it is also passed in the link, you can just add it to the URL as a query string, like:
http://..../blah.php?var1=val1&var2=val2
as long as the data you're passing can fit into a URL. Be sure to urlencode() anything you're appending to the URL.
<?php
start_session();
$_SESSION['Foo'] = 'Bar' // Add stuff here.
?>

Categories