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.
Related
I have a search form, and the submit button looks like this:
<input type="submit" name="search_submit" value="Go"/>
I handle this form using the following php:
if (isset($_GET['search_submit'])) {
do blah
}
Works fine. But my url then includes &search_submit=Go. I do not want that to show up.
I know that to fix this, I need to remove the name attribute from my the forms input line.
But then my php no longer works and I'm not sure how to change it to handle the form. I tried changing it to:
if (isset($_GET['submit']))
and
if (isset($_GET['Go']))
But they did not work either. If anyone can help me with an answer, it would be awesome.
You cannot remove the name of the input element, as PHP would not know which value to look for. If you want to completely hide the string after the URL, use the request method POST instead of GET:
<form action='myscript.php' method='POST'>
<input type="submit" name="search_submit" value="Go"/>
</form>
Your PHP will use the following:
$_POST['search_submit']; // Instead of $_GET['search_submit'];
A good answer to when to use GET and POST can be found here.
edit: If you just want to not have the button show up in the URL, but everything else should still be there (according to your comment), you can simply remove both the value and name of the submit button.
Instead of looking for search_submit to be set, you can look for the other values:
if (isset($_GET['username'], $_GET['password'])) {
// Do your stuff here
}
If you don't want to show string in the URL, you can use the POST method. The main difference between GET and POST are listed below as :
GET:
Parameters remain in browser history because they are part of the URL
Can be bookmarked.
GET method should not be used when sending
passwords or other sensitive information.
7607 character maximum
size.
Url example: new.php?category=sport
POST:
Parameters are not saved in browser history.
Can not be bookmarked.
POST method used when sending passwords or other
sensitive information.
8 Mb max size for the POST method.
URL example: new.php
Sample Code :
if (isset($_POST["search_submit"])) {
do blah
}
If the submit button doesn't have a name, then it won't be a successful control and won't appear in the submitted data at all.
Test for the presence of data from some other field in the form instead.
All the above code I saved in a page called dum.php
$(document).ready(function(){
$(".sameclass").click(function (e) {
e.preventDefault();
var QS = $(this).attr('href').split('?');
if(QS.length>0){
var val = QS[1].split('=');
if(val.length>0){
$("#edit_prdct").hide().slideDown('slow');
//Below am adding a value to the hidden text filed
document.getElementById('prd_id').value=val[1];
}
}
});
});
I had some products which i want to edit their info EDIT are the hyperlinks with the query string value
<DIV id="list_prdct">
<a href="dum.php?id=1" class=sameclass>EDIT</A><BR><DIV id="edit_prdct">
<a href="dum.php?id=2" class=sameclass>EDIT</A><BR><DIV id="edit_prdct">
<a href="dum.php?id=3" class=sameclass>EDIT</A><BR><DIV id="edit_prdct">
<a href="dum.php?id=4" class=sameclass>EDIT</A><BR><DIV id="edit_prdct">
</DIV>
In the below div I used a hidden field and I want to used its values in the mysql query in the where condition and I will display a form related to that value
<DIV id="edit_prdct">
<?PHP
$prdid"<input type=hidden id='prd_id' value=''>";
// Here I want to use that value of the hidden field as the condition in the mysql query
?>
I am not sure if I got your question correctly, however, this is my try.
Every system has 3 parts: input, processing and output.
Here, your PHP logic is the processing part. It runs on the server and understands only Php. It doesnot know about your HTML, CSS, JavaScript, etc.
Input is provided through HTML and Javascript(JQuery) using a web browser. When you click a link, or submit a form, or make an Ajax call, then browser basically sends an input to the web server (its called a HTTP Request)
Output is again HTML, CSS and Javascript. The statements you echo or print including the ones outside <?php ?> tag, are all output. These are understood by a web browser and not by the server.
To send any data from client side (the web browser) to the server, you need to use HTTP requests. This can be done in 3 ways:
Synchronous Request(href redirects, form submission, sync. Ajax calls, etc)
Asynchronous Request (Ajax Calls).
URL redirections (its a form of way 1 above!!)
All the ways above typically use either the GET method (query strings) or the POST method.
If you dont want to use Ajax or form submission, you are left with only third option, that is, call the resource links directly. For example, redirecting the user to some URL like:
http://www.example.com/products/134
where 134 is a prod_id.
Then on the server side, you will have to retrieve the URL and extract out prod_id from it. This is a tedious task. Fortunately we have some good frameworks to do that, like CodeIgniter, etc. But this is not the way to send data to the server in case you need to fetch data for a list of prod_id.
So, No, there is no other way to get data from server. Perhaps you need to remodel your problem and understand that Php server can't know about your prod_id magically, you need to send it through one of the methods of HTTP request
I have an xml feed that I can access using simplexml_load_file and using for each access all of the records in it. I am also able to access an individual node using the following:
$xml->property[0]->propertyID;
What I want to be ble to do is to indvidually display each record in turn, ie move from [0] to [1] and so on in turn on page refresh but don't know how to go about that.
I'm just a hobbyist so please forgive me if this is a bit of newb question
You need to introduce some state somewhere, so that you can remember where you've got to.
You can either have the browser count up each time the page is refreshed using a cookie, or you can remember this on the server side by storing the current node number somewhere - in a database, in a file, or in the URL.
Probably the simplest thing to do is to tack the next node number onto the end of the URL, so when that page is reloaded, you'll see that parameter on the server side and can load that node. Something like this:
$node = 0;
if (empty($_GET['nextnode']))
{
header('Location: example.php?nextnode=' . node + 1);
} else {
$node = $_GET['nextnode'];
}
...
$xml->property[$node]->propertyID;
When the page is refreshed it will make a request to your PHP script, as normal, but it'll have something like this on the end of the requested URL:
?nextnode=23
PHP will store these URL parameters in the $_GET array, which you can then query in your script. Look up the documentation on header for more information.
Another, similar, technique is to store the value in a hidden form field when you create the page in PHP:
echo '<form>
<input type="hidden" value="'.$node + 1.'" />
<input type="submit" value="Next Node" />
</form>';
This will be create a 'Next Node' button on the page, and will get sent back to you as a form submission when someone clicks it. You can then lookup the next node and redisplay the page. See here for more info on dealing with forms in 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.
Let's say I have a page called display.php and the user is viewing display.php?page=3. I want to allow the user to do an action like voting via a POST request and then bring them back to the page they were on. So, If I do a POST request to display.php?page=3 would the page information also be available to the script?
The simple answer is 'yes'. You can use a GET-style URL as the submission URL for a POST form. PHP will have both the POST and GET information available to it in the usual ways when the form is submitted.
This is not to say that you should do this, but it will work.
In PHP, you can get request variables from the special global arrays:
$_GET['page'] (for GET requests)
$_POST['page'] (for POST requests)
$_REQUEST['page'] (for either)
It sounds like you are looking for "Redirect after Post", I would suggest separating display.php and vote.php into separate files. Vote looks something like this:
<?php
//vote.php
$page_number = (int)$_REQUEST['page'];
vote_for_page($page_number); //your voting logic
header('Location: display.php?page=' . $page_number); //return to display.php
Note that blindly accepting unsanitized form data can be hazardous to your app.
Edit: Some folks consider it bad form to use $_REQUEST to handle both cases. The hazard is that you may want to signal an error if you receive a GET when you expect a POST. Typically GET is reserved for viewing and POST is reserved for making changes (create/update/delete operations). Whether this is really a problem depends on your application.
Yes, the GET array is always filled with the URL parameters regardless of the request method. You can try it with a simple page like this:
<form action="test.php?a=b" method="post">
<input name="a"/>
<input type="submit"/>
</form>
<pre>
POST:
<?php print_r($_POST); ?>
GET:
<?php print_r($_GET); ?>
</pre>