• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 658
  • Last Modified:

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???

  • 3
  • 2
1 Solution
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 domain.com/invoicing/12/20070712.

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.
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....

socrossAuthor Commented:
Great stuff



Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now