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

Posted on 2007-10-14
Last Modified: 2008-01-09

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

Question by:socross
    LVL 34

    Expert Comment

    by:Beverley Portlock
    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' />

    LVL 1

    Author Comment

    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


    LVL 34

    Expert Comment

    by:Beverley Portlock
    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.
    LVL 34

    Accepted Solution

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

    LVL 1

    Author Comment

    Great stuff



    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
    Creating and Managing Databases with phpMyAdmin in cPanel.
    In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
    In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now