Refine results using form (POST or GET) with mod rewrite


I need some advice with a form which refines results in a database query.

The page simply pulls results out of a page and then displays the results to the user, i want a form which give the option to select specific detials (client name, status and time period ) and then refine the results, I have written all the code which does the sorting (using $_GET) but want to know the most effcient way to get the data from the form.

I also have to concider that throughout the site i am using mod rewrite so i have clean urls, so simply using GET from a form may not work out because the url will not be in the correct format (ie page/id/ etc)

WOuld i need to post to another page and then redirect back to the clean url, or can i use post???

Who is Participating?
Beverley PortlockCommented:
OK - the "refining the search" bit. IN essence, what you need here is to be able to build your "where" clause. When you run the initial query you will have something like

select * from table where item='abcde'

Ideally, you want to be able to extend that basic query and rerun it to produce a subset of results, so if someone wanted the items in blue

select * from table where item='abcde' and colour='blue'

and then if they wanted items that cost less than 20

select * from table where item='abcde' and colour='blue' and cost < 20

So as you refine the search you need to extend the where clause. That means you need to store the existing criteria somewhere and that somewhere can be

1) in a cookie
2) in a session variable
3) in the URL and  in the form as hidden vars

my preference is number 3 as it does not depend on javascript or client side settings. Also, new items to refine the search terms just pop-in to the URL

So, in PHP, check if each of your search terms
if it exists in the URL then
     make it a hidden field on the form $hiddens = "<input type='hidden' name='myField' value='$myValue' />";
     add it to the where clause $where .= "$myField='$myValue' and ";

Chop the end off the where clause to lose the trailing " and "
$where = "where " . substr($where, -5);

Add to the SQL

$qry = "select * from table $where"

Pop the hidden fields into the form

<form ..... >
<?php echo $hiddens; ?>

and off you go....

Beverley PortlockCommented:
If you are using mod_rewrite correctly then it will be reformatting the URLs into their correct format regardless of how they are displayed. So that should not be a factor.

I'm not clear from the rest of your comments whether you've written a form already or want to write a form.

The only determining factor about using POST and GET for the sort of form you are talking about is the amount of data involved. $_GET can only cope with less than 2K, post usually goes up to 2M.

The simplest thing is to divide your PHP script into sections as follows


    // Collect any variables and ensure they are escaped
    // have one of these for each variable you need
    $var1 = ( isset($_GET['var1']) ) ?  mysql_real_escape_string($_GET['var1']) : "";

    // Deal with a submission
    if ( isset($_GET['submit']) ) {

        // validate any data and write it to the database
        // Go somewhere else if you need to

    if ( $errors != "" )
        display errors

    // Perform any other needed PHP stuff like build <select> tags


<!-- HMTL goes at the bottom of the script -->
<form action='<?php echo $_SERVER['PHP_SELF']?>' method='get'>
..... your form fields
    <input name='submit' type='submit' value='Search' />

socrossAuthor Commented:
Sorry ive not been clear with my question

Have look at the ebay, with any page that displays listings you have aditioanl 'search options' down the left to refine the results displayed. This is a much more complicated version of the result i am looking for, i just need to know the best way to go about it.

the page is finished and uses $_GET values to refine results and i have tested it by testing it with hand written urls like

mod rewrite means this is the url i would want to see, so how would i link to this from the form,

Or do you think im just better off using posted vars thats fine by me but i just wasnt sure on what people gennerally do.

Any Ideas


Beverley PortlockCommented:
To answer the rewrite question, you would write the URLs for people to click on in the format you want them to see. In your case

You would then add a rewrite rule to your virtualhost (assuming Apache) that looks like this UNTESTED one below

RewriteEngine On
RewriteRule ^/(.+)/(.+)/(.+)$ /$1/index.php?id=$2&date=$3 [L]

That way when someone clicks on for formatted URL Apache translates it into the form you need.
socrossAuthor Commented:
Great stuff


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.