在页面上获取请求删除POST信息

I have a page called test_photo.php, which people get directed to from a form after doing a POST request

<form id="search_photos" action="test_photo.php" method="post">
    <select name="photographer" id="photographer">
        <option selected="selected" value="x">Any Photographer</option>
    </select>

    <--Removed for clarity sake -->

    <input name="Submit" value="Search Now &gt;" id="Submit" type="submit">
</form>

on the page that we post to (test_photo.php) we have pagination to help break up the huge amount of data that gets displayed. This is handled by a GET request the url may look something like http://foobar.com/xpath/test_photo.php?page=0.

The problem is that whenever someone perform a get request on the page the POST data disappears. Does anyone know any way in which we can solve this.

Okey, trying to rule out the obvious here.

You are mixing GET and POST in a way that is incorrect. You should only address POST if you have a form with values you don't want exposed and those values are no longer needed after submitting (like logging in). Here you have values that you carry on to the next page etc.

This will never work because if you post a form, the POST-values are only available right after you submitted the form. If you try to do a clean refresh (not reposting the form), you will see that the values are gone.

What you should do it posting the form as POST and then building a query of GET-parameters.

Like

page.php?page=1&photographer=1&location=3

This is a correct way to address filters. Just look at how Google is storing the current search-query (google.com?q=your search). The rule is "values needed across different pages"? Just GET. This will also make it possible for people to bookmark searches or send them to other people with all the filters applied.

First of all, the POST data does not disappear. It is there, but your submit handler probably ignores it if it sees GET data first.

As this little demo will show:

<?
    print_r( $_REQUEST );
?>

<form action="/path/to_this/page.php?get=yes" method="post">
    <input type="text" name="test" value="SomeVal"/>
    <input type="submit">
</form>

But, that said, it sounds like your simplest option is to submit the data to the page without its GET parameters. I am assuming, you do not need both (i.e. you do not care what is the page number in the case of a POSTed data).

But, in any case, you should be able to view both GET and POST data.

Now, when you say "someone perform a get request on the page" - if you mean, someone pastes the URL in a browser and hits Enter, then you will never see that POST data in this case. You will only see it after the form is submitted.

I didn't think a GET request would destroy your POST data. Check to see if your $_POST data is intact when the "destination" page loads by adding a debug statement such as print_r($_POST); to your page. It should print out the entire $_POST array to inspect.

If your browser supports such, a right mouse click may allow you to "inspect element" to see the array in a better formatted view.

You can do the same with print_r($_GET); to see what it contains as well, or any array in fact.

GET values don't destroy POST ones, you have to understand this a little better, that's all.

When you submit your form using POST, the page that loads generates a $_POST array with the values passed. When you load another page including GET parameters, POST disappears because it is not persistent across page loads.

What you have to do is to store the values (using sessions, for example) to make them persistent. Something like:

<?php
    session_name("any_name_for_starting_a_session");
    session_start();

    if (isset($_POST)) $_SESSION['passed_values'] = $_POST; // Store the values if POST is sent

    if (isset($_SESSION['passed_values'])) { // Getting the values if stored
        $post = $_SESSION['passed_values'];

        // Here you can work with $post ...
        //...


        // If you need to delete the stored values
        // unset($_SESSION['passed_values']);
    }
?>