Can I retain search values on results page?...

Discussion in 'PHP' started by sstoney200, Apr 18, 2011.

  1. #1
    Hi guys!

    I've got what appears at first glance, a niggle more than a problem, and was wondering if anyone could think of a way of solving it, assuming, that is, that it can be solved...

    I have a search facility on my website that searches an xml doc and returns the results via php to my results page. My results page is located: http://www.for-rent-nerja.com/search.php

    Basically, if you search with everything as it is but change the bedrooms to 2 it returns the correct results but resets the form to the original 1 bedroom value. I want the form to retain that 2 bedroom field if possible (and anything else the user may have changed). It sounds simple enough but like most things I'm guessing it really is not!

    This is the php as is that returns the search:

     <?php
       $xml = simplexml_load_file('property_catalog.xml');
            $results = array();
           
            $query = '/CATALOG/PROPERTY';
           
            if(isset($_POST['DURATION']) && strlen($_POST['DURATION']) > 0)
                $query .= '[DURATION="'.$_POST['DURATION'].'"]';
           
            if(isset($_POST['TYPE']) && strlen($_POST['TYPE']) > 0)
                $query .= '[TYPE="'.$_POST['TYPE'].'"]';
           
            if(isset($_POST['AREA']) && strlen($_POST['AREA']) > 0)
                $query .= '[AREA="'.$_POST['AREA'].'"]';
           
            if(isset($_POST['BEDROOMS']) && strlen($_POST['BEDROOMS']) > 0)
                $query .= '[BEDROOMS="'.$_POST['BEDROOMS'].'"]';
                   
            $results = $xml->xpath($query);      
              
            echo '<h1>Results ('.((is_array($results) && count($results) > 0) ? count($results) : 0).')</h1>';
            if(is_array($results) && count($results) > 0)
            {
                foreach($results as $property) {
                    echo '<div class="price t_color1" > '. $property->PRICE .' </div> <div class = "t_color1 t_bold"> '. $property->BEDROOMS .' '.$property->TYPE.' for '.$property->TERM.' '.$property->AREA .' </div>  <div class = "content1_img float_l"> 
                    <a href="'. $property->URL .'"><img src="'. $property->IMAGE .'"></a> </div> <div class = "content1_text float_l"> <b class="t_color5">Property</b><br /><br /> '. $property->DATAPROPERTY . '</div> <div class = "content2_text float_2"><b class="t_color5">Features</b><br /> <br /> '. $property->DATAFEATURE .  '</div> <div class = "left_col float_l"><p><img src = "http://www.for-rent-nerja.com/images/spacer2.jpg" alt = "" width="500"/></p></div>' ; 
                    
                }
                
            }
    ?>
    PHP:
    Now I know it's resetting the fields because it is loading the page from afresh but I was wondering if there was some code I could add to this php to retain the search fields? Any words of wisdom appreciated! ;-)
     
    sstoney200, Apr 18, 2011 IP
  2. AdM.as

    AdM.as Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There certainly is :). What you have to do is echo the POST variables for each field, for example:

    Say you have an input field for the 'BEDROOMS' variable, it would probably look something like this:

    <input type="text" name="BEDROOMS" value="" />
    PHP:
    All you have to do is echo out the 'BEDROOMS' variable from POST into the value field, like so:

    <input type="text" name="BEDROOMS" value="<?php echo $_POST['BEDROOMS']; ?>" />
    PHP:
    Copy this for all of the fields you wish to save and you should be good to go :)
     
    AdM.as, Apr 18, 2011 IP
  3. sstoney200

    sstoney200 Greenhorn

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Hey buddy! :)

    Thanks very much for your response!

    OK so on my search form field for my bedroom field I have:
    <select id="BEDROOMS" name="BEDROOMS" value="<?php echo $_POST['BEDROOMS']; ?>"> 
    Code (markup):
    however that does not appear to be holding the value, even though if I read the source code on the searched page it is returning:
    <select id="BEDROOMS" name="BEDROOMS" value="4 bedroom">
    Code (markup):
    or whatever bedroom value I enter.

    Am I missing something or should it be working? :confused:
     
    sstoney200, Apr 18, 2011 IP
  4. AdM.as

    AdM.as Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ahh ok select tags work a bit differently

    Your select should be set up like this:

    <select id="BEDROOMS" name="BEDROOMS">
         <option value="1 bedroom"<?php echo ($_POST['BEDROOMS'] == '1 bedroom') ? 'selected="selected"' : ''; ?>>1 Bedroom</option>
         <option value="2 bedroom"<?php echo ($_POST['BEDROOMS'] == '2 bedroom') ? 'selected="selected"' : ''; ?>>2 Bedroom</option>
         <!-- add rest of options same way -->
    </select>
    PHP:
     
    AdM.as, Apr 18, 2011 IP
  5. sstoney200

    sstoney200 Greenhorn

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    Thanks mate! Super helper! Works great! :)
     
    sstoney200, Apr 18, 2011 IP
  6. AdM.as

    AdM.as Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Glad I could help :) Also something I noticed in the last code I posted, there should be a space included in the echo, so this part:

    <?php echo ($_POST['BEDROOMS'] == '1 bedroom') ? 'selected="selected"' : ''; ?>
    PHP:
    Should be:

    <?php echo ($_POST['BEDROOMS'] == '1 bedroom') ? ' selected="selected"' : ''; ?>
    PHP:
    Notice the space before selected.
     
    AdM.as, Apr 18, 2011 IP
  7. sstoney200

    sstoney200 Greenhorn

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    I have amended the code with the space.. Thanks again for your time, very much appreciated! ;)
     
    sstoney200, Apr 18, 2011 IP