页面返回时的PHP搜索结果

I'm currently display the search results in the same page (index.php), in the parameters of URL I just put index.php?type=search

<form method='POST' action='" . $_SERVER['PHP_SELF'] . "?type=search'>

So, in the index.php I check if it's search or not:

if(!isset($_GET['type'])) {
    echo $load->common();
}else{
    if($_GET['type'] == 'search'){
        if(isset($_POST['search-what-box'])){
            echo $load->search($_POST['search-what-box'], $_POST['search-where-box'], null);
        }else{
            echo $load->common();
        }
    }
}

As you can see I make several verifications. But imagine that people enter in another url (of my page), like list.php?id=33 and then they click on Back button of the browser..the browser will display (with this code) the results of $load->common() and not the $load->search(), because the $_POST['search-what-box'] wasn't pressed (the form wasn't submited).

How can I show the results of search instead of common results after people click on back button of browser? Should I save the information (after submiting the form) in javascript and then load it again by jQuery or there's a better approach?

Thanks.

In general you should design your website that flexible that the back button is not nesessary at all.

But if you really want to save your last search result you can save it in $_SESSION for example on the server's side.

you may use a JQuery Ajax call see here

if click back the Get and Post data information are the previous ones in this case, ajax then uses that information to render the page

Unrelated to your question, but you mix $_GET['type'] and $_POST['search-what-box']. While technically possible, you should use either $_GET or $_POST.

To solve the "Back button problem", you might use the Post/Redirect/Get idiom.

First question is what the behavior of browsers. As far as I can see, browsers are not loading the page anew when you pressing back - they show you the page itself as the server sent them before (with the post data).

Why don't you make the search query part of the URL? Use _GET instead, this is what Google.com does. That way I could paste out the URL and give it to someone and it'd view the results.

Ok guys, with all your answers and help I solved my problem.

$url = "http://". $_SERVER['HTTP_HOST'] . urldecode($_SERVER['REQUEST_URI']);
// urldecode = some special characters might be in the query

$parse = parse_url($url);
// documentation: http://pt1.php.net/parse_url

if(isset($parse['query'])){
    $split = $parse['query'];
    parse_str($split);

    // documentation: http://pt2.php.net/manual/en/function.parse-str.php

    if(isset($what)){
        echo $load->search($what, $where);
    }else{
        echo $load->common();
    }
}else{
    echo $load->common();
}

If anyone think there's a better approach, let me know.

Thanks