SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    British Columbia, Canada
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to clear/refresh a search form

    I've been reading the online php documentation but I find it to be over my head a lot of the time, so thought I'd ask this (probably) simple question here.

    Rather than putting a note on the search page instructing the user to hit reload/refresh in their browser each time they land on the page using the back button, I'd like to include some coding to make that happen automatically.

    I've tried including: <meta http-equiv="pragma" content="no-cache"><meta http-equiv="expires" content="-1"> in the head area but that doesn't clear the fields. I've also tried adding (instead of the previous code) <meta http-equiv="refresh" content="600"> but that doesn't work either.

    Any suggestions? I know this is more an html question than php but since the search fields are inside php tags I figured I'd better ask here, just in case.

  2. #2
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    Hi Helen,

    I know that you are asking about this in a PHP forum, so you could use a form button called 'clear' with the only input in the form being hidden. and post back to Self, then the form that had the values would clear.

    If you were willing to use javascript you could use AJAX and clear the values without having to refresh/reload the page.

    Steve
    ictus==""

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    British Columbia, Canada
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Steve. Does everyone enable javascript these days or are there still people around who turn it off? If *everyone* uses it I have no objections to using it to clear the form. I'd really like to avoid any kind of manual clearing. I know my audience.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    Do both. Those with javascript enabled will have less to do, and those with it disabled are more than likely used to needing to do "extra".

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    British Columbia, Canada
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mittineague. I've found the following javascript, which is supposed to trigger when the user leaves the page but it's not working for me. I've tried it in the <head> area and also just before the </body> tag:

    <script type="text/javascript">
    window.onbeforeunload = function() {
    window.name = "reloader";
    }
    </script>

    I've also looked for some AJAX coding, as Steve suggested, but all I can find are ones which keep refreshing every x seconds, which isn't what I need. I just need a search form field to be cleared if a person uses it to find something, then hits the back button to go back to the search form. There are 5 different options on the search form but they can only search for one at a time, which is why I need the previous one to automatically clear if possible.

  6. #6
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    Hi Helen,

    Here is one solution that uses both.
    PHP Code:
    <?php  session_start(); ?>
    <script language="javascript" src="test7.php"></script>
    <?php 
     
    if( $_SESSION['js_clear_active'] == FALSE) {?>
      <form id="my_form" action='./next.php'>
        First name: <input type="text" name="fname" /><br />
        Last name: <input type="text" name="lname" /><br /><br />
        <input type="submit" name="submit" value="Submit"></a>
      </form>
      </form>
       <form id='php_clear_form' action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" accept-charset="utf-8"> <!-- When this posts it reloads the page -->
       <input type="submit" name="clear" value="Reset Form"></a>
      </form>
     <?php } else {?>
    <form id="my_form" action='./next.php'>
      First name: <input type="text" name="fname" /><br />
      Last name: <input type="text" name="lname" /><br /><br />
      <input type="submit" name="submit" value="Submit"></a>
      <input type="button" name="reset_form" value="Reset Form" onclick="this.form.reset();">
    </form>
    <?php }?>
    You use the
    HTML Code:
    <script language="javascript" src="test7.php"></script>
    To determine if JavaScript is turned on. It run only if JavaScript can parse the script tag and will call a simple php page test7.php:
    PHP Code:
    <!-- test7.php -->
    <?php 
     session_start
    ();
     
    $_SESSION['js_clear_active'] = TRUE;
    ?>
    If you test it with JavaScript turned on first then to test that Javascript off works you will need to issue a $_SESSION['js_clear_active'] = 0; command to clear the session; otherwise your script clear button will appear but have not javascript support. Once you issue $_SESSION['js_clear_active'] = 0; then comment it out and try with Javascript off.

    If you decide to use this then you should get the session to clear more cleanly.

    Most people will have JavaScript either on or off, but there are those that disable it on the fly, so by cleaning the session if Javascript is not turned on then even the 'change on the fly' crowd will get a way to clear the form.

    Hope this helps,
    Steve
    ictus==""

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    British Columbia, Canada
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh - wow! Thanks Steve! I'll try and get my brain around it and add it to the page. It sounds perfect, if I can figure out where everything belongs. I'm still on a steep learning curve with this stuff but it becomes clearer every day.

  8. #8
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    Hi Helen,

    Yes yucky learning curves getting in the way of our brilliant ideas

    Here are a few things to consider:
    • The session_start() must go at the top of your php page before anything has outputed. This means that you can't have a space after '<?php' or you'll get an error 'headers already sent...'
    • This is not AJAX, but that gets more complicated so I kept it simpler
    • You run the <script language="javascript" src="test7.php"></script> before the forms and the php branching logic.
    • You can run a <noscript> tag directly after the <script> tag that calls the php. In the <noscript> tag you can put <?php session_start(); $_SESSION['js_clear_active'] = 0; ?> in the javascript_is_off.php file like:
      Code:
      <script language="javascript" src="javascript_is_on.php"><noscript language="javascript" src="javascript_is_off.php">
      . If JavaScript is turned off, this <noscript> will run and set the session to 0 so the branching logic will select the non-JavaScript code. This is the session cleanup that I referred to in my last post; you have enough to learn


    There are other ways to do this, however this is the most PHP way I could think of, others may suggest other ideas, all that I can say as I wrote and tested this and it works

    Regards,
    Steve
    ictus==""

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    British Columbia, Canada
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Steve! Sorry to take so long to acknowledge - I got sidelined onto another part of the same project and have just come up for air from that. Thanks so much for taking the time to clarify; it makes MUCH more sense to me now.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •