php $_POST and pagination $_GET - php

I face a problem whenever the user tries to browse to second page via $_GET if they have submitted $_POST data.
if(!isset($_POST['submit'])) {
//search input box
}
else {
//search details output
//pagination code
}
Whenever user press page 2, it shows //search input box back.
I want search to show page 2 and not //search input box back.

This is because you are not sending POST data to the second page.
$_GET and $_POST are set per request. If you want to save the first POST data, you will need to use sessions and store it in the session, or return the POSTed data to your page and have it be resubmitted.

As #Alan says... page 2 isn't receiving POST data, so submit is not set and it thinks the starting form should be shown again. A second GET variable (eg page) to track the results-page, will allow all three pages (start, page1+submit, page2)
if (isset($_REQUEST["page"])) {
//No data received, but reviewing & page-display code should go here
} elseif (isset($_POST["submit"])) {
//POST-processing code goes here
// Page-switching URL should be something like:
// Page 2
} else {
//Nothing posted, not paging - show input
//search input box
}
Don't forget to store your POST data somewhere temporarily so that you have data to display on the pages.

Seams like your logic is twisted. Just look at your code with aditional comments:
// if POST submit is NOT set
if(!isset($_POST['submit'])) {
// show search input box
}

You shouldn't be submitting a search request via POST. Searching is the kind of read-only operation ideally suited to GET requests and the query string. All you have to do then is modify the query string to include something like &page=2 to add pagination to your links.

A better way to do it is to split your form processing into its own script. This makes it more organized and more logical so that you can do things like POST to it via AJAX easier. A tip for solving the other problem I see you are having is if you are having a multipage form you should store the data they submit on previous pages somewhere. A common place is in session data. There are other ways to do it like storing it all in a javascript data object until the form is completed. Look into using a framework, they will help.

Related

php get the post data when using pagination script

I have a page called mainPage.php that contain a pagination script which displays a list of pages.
[1][2][3][4][5]
the pagination works fine but when I go to the next page the post data disappear.
for($i=1;$i<=$totalPage;$i++)
{
if($page==$i)
{
echo ' <input type="button" value="'.$i.'"> ';
}
else
{
echo ' <input type="button" value="'.$i.'"> ';
}}
is there any way to get the post data to the next page number after clicking this link:
<a href="mainPage.php='.$i.'">
without the form.
No, it's not possible. When a form is submitted (with method=post) to the server, that's one POST request. If you want to make another POST request, you need to make another POST request. If you click a link, that's not a POST request.
I'm assuming your scenario is something like a search form, which is submitted via POST and which returns several pages of results. In this case, POST is misused anyway. An HTTP POST request should be used for altering data on the server, like registering a new user or deleting a record. Just a search form is not data alteration, it's just data retrieval. As such, your form should be method=get. That will result in a URL with the form values as query parameters:
mainPage.php?search=foobar
You can then trivially create pagination URLs from that:
printf('<a href="mainPage.php?%s">...', http_build_query(array('page' => $i) + $ _GET));
Which will result in:
mainPage.php?search=foobar&page=2
This way all your requests are self contained GET queries.
try <a href="mainPage.php?page='.$i.'"> and get page no. using $_GET['page'];
You can do your job by using the GET method instead of post.If you want to make it in post request you need to do it seperate for each one.

submit a form when hitting the browser back button

I have a page for asking queries to an SQL database. Its only purpose is to allow students to exercise. Depending on the students activity the page rewrites itself with new content so that the student may enter a query, have the resulting table shown or get an error message.
All is working through forms that post data to the same page.
However, if a student uses the back button or the forward button (after hitting the back button) data gets lost as I cleanse the $_POST variable content to get ready for new action.
There is, however, a "go back" button that assembles data to restore the previous page by POSTing the required data. Is it possible to use some kind of technique, javascript, html5, PHP or whatever to actually submit the form that posts the assembled data when hitting the browser back button?
I am using HTML 5, PHP 5 and some JavaScript (not JQuery but if it gives me an option ...)
you can use the html5 storage since if user not fill the full form or close the browser the data will lost on close browser and not submit it form not fill
to check html5 storage
function supports_html5_storage() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
use onkeyup to store like
$("#title").keyup(function(){
var articel_title = $("#title").val();
localStorage.setItem("articel_title",articel_title);
localStorage.getItem("articel_title");
});
and next time when user open the form just show the content stored
to clear use
localStorage.removeItem("articel_title");
As suggested in the comments you could store the post data in the session, for example every time a new query is posted you could add it:
$_SESSION['queries'][] = $_POST;
Then you could allow the users to go back / forward through this with some form of loop:
<ul>
<?php foreach($_SESSION['queries'] as $k => $v) : ?>
<li>Some link structure</li>
<?php endforeach; ?>
</ul>

Retaining data in HTML form

I have an HTML form. Let's say I fill out all the fields and submit it (PHP script runs here).
Then I want to go back to the form using "Back" button of my browser.
What I see is the empty form.
What do I do to have the entered data retain on the page after I come back to it using "Back" button of the browser?
Thank you!
If you use the "Back" button of your browser then your browser is responsible for re-populating your form data.
Usually that functionality is handled by the browser, however if you want to "force" the fields to always be pre-filled with the user's data, you can store the $_POST data in a session variable and use that to load the form.
Example:
// submission page
session_start();
if(isset($_POST)){
// save the posted data in the session
$_SESSION["POST"] = $_POST;
}
Then on the actual form page, you can check to see if session data exists. It won't if the form is being loaded the first time, but it will if the user submits the form and then presses the browser back button:
// form page
session_start();
if(isset($_SESSION["POST"])){
// previous POST data has been saved
// build the form with pre-defined values from $_SESSION
...
} else {
// no previous data
// build the form without pre-defined values
...
}
Note that you must call session_start() before outputting any HTML.
Store the value in a session
session_start();
//form so that you have all the potential forms in a single session array
//form_1 to identify the form in question
if(!empty($_POST)){
$_SESSION['forms']['form_1'] = $_POST;//if this is for the public internet, then I would really consider making sure that the posted data matches the received data... (and that its comming from YOUR form), which is way too long to post here...
}
then on the form page
<input name="flowers" value="<?php echo if(isset($_SESSION['forms']['forms_1']['flowers'])){ echo htmlspecialchars($_SESSION['forms']['forms_1']['flowers']);} ?>" />
obviously the above can be simplified, but for a example's sake it's better this way.
(make sure to clean out the old form data eventually)
You can potentially store the data in the session, and re-populate it back using PHP sessions. You should create a separate back button that takes you to the previous page.
Example:
Storing data:
$_SESSION['data'] = $_POST['item1'];
In the HTML Forms:
<input type="text" name="someinput" value="<?=$_SESSION['data']?>" />

how to submit a form and show the result in that page

im using a form in php to submit some information into my database
so i used two function to do this
but how to show the result in th same page that has the form
To load the same page you have to assign the variable $_SERVER[PHP_SELF] for the form action field.
<form action='$_SERVER[PHP_SELF]?op=ban' method='post'>
then when the page get load you just check the post variable ,if it contains the appropriate data then print the result with the form.(Normally people using div tag to print the results )
It's as easy as this:
if (isset($_POST['submit']))
{
// do something with your data
}
form();
Forgive me if I am wrong. I think you have copied the code from some where and using it without understanding how forms work.
<form action='index.php?op=ban' method='post'>
The above code says to which page the values should be submitted. As you can see above the values in the form will be submitted to index.php. So the DB operations will(should) happen in index.php and the Thank you message can be shown in index.php.
If you want to show your result in the same page then you will have to submit to the page in which the form resides. But in this case you should have a logic in the page to decide whether the form was submitted or was it loaded first time.
The code snippet in your question does not tell us name of the file the code exists so we wont be able to tell you whether the result will be shown in the same page. Aslo the source code is not complete.
Post a detailed source code and we will be able to help. Hope it helps.
it should be shown on the next request.
because your app should perform an HTTP redirect after POST request.
it can be same page though

PHP Multiform Validation and Redirection

I have buy.php with a form where you enter items, quantity, shipping data, etc.
When you click the Submit button, it posts back to buy.php ($_SERVER['PHP_SELF']) and does some data validation.
If there are fields missing or errors, they are highlighted. If everything is correct, I save the $_POST data in $_SESSION variables, then do a header('Location: check.php'), where I display the data so the buyer can check the info one last time before actually buying.
Now, if I'm in check.php and hit the Back button to buy.php so I can change stuff, the browser asks if I want to resend the POST data. I'm trying to avoid that.
Anyone have any good advice or good practices for PHP Multiform validation?
Also, if I had n pages for the user to fill, buy.php, buy2.php, ... buyn.php before check.php would the same ideas still hold?
You could do a redirect to buy.php after saving to the session object, which then does a server redirect to check.php, it would mean when the user clicks back, they're going back to the GET request not the POST request
Yes - I agree with above. I ALWAYS do a redir away from the last post, so clicking back bounces them back without that error OR re-submissions. it also avoids complications. u can always tag the redir link page with a ?m or &m (i.e.: page.php?m) and have this at top of page: (use elseif there after)
if (isset($_GET['m'])) {
echo 'order placed.';
}
else {
//...
}
You can have it all on one page too. Just name the submit buttons submit1, submit2, like: (bear in mind if you use an image for submits, it becomes $_POST['submit1_x'] :)
if (isset($_POST[submit1]) {
//validate + save session data from form1
//display form 2
} else if(isset($_POST[submit2])) {
//validate + save session data from form2
//display form 3
} else {
//display first form
//<input type="submit" name="submit1" value="Continue">
}

Categories