Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts

    Clearing variables after being submitted through a form

    Hi,

    I would like to clear the variable contents after my script has processed to stop people from refreshing the page and spamming my database. What happens is that I have info that is passed through a form, and when its all done you can still refresh the page and resubmit the data. I would like to have the variables cleared at the end of the script so that if a person refreshes the page, the error for an empty field will show up because the variables have been cleared.

    I've though of just doing something like:
    PHP Code:
    <?php
    $blah 
    $_POST['blah'];
    $food $_POST['cookie'];
    $sql mysql_query...
    if(
    $sql){
    //insert...
    }
    //clear variables
    $blah "";
    $food "";
    ?>
    To check for blanks I implement this:
    PHP Code:
    <?php 
    $blanks 
    = array();
    if(empty(
    $name)){
       
    $blanks[] = "Error no name...";
    }
    if(empty(
    $food)){
       
    $blanks[] = "Error no food...";
    }
    if(!empty(
    $blanks)){
      foreach(
    $blanks as $error){
        echo 
    $error;
      }
    }
    ?>
    Does anyone have a more efficient method for clearing the variables?

    *EDIT*
    I just thought about unset(), what about that?
    Last edited by fl00d; 09-01-2007 at 03:32 AM.

  • #2
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    How about using a session?

    PHP Code:
    if(!$_SESSIONsession_start();

    if(!isset(
    $_SESSION['insert_ok']) || $_SESSION['insert_ok'] == 0) {
        
    $blah $_POST['blah'];
        
    $food $_POST['cookie'];
        
    $sql mysql_query...
        if(
    $sql){
        
    //insert...

        //insert successful? Flag it in a session
        
    if($sql) {
            
    $_SESSION['insert_ok'] = 1;
         } else {
            
    $_SESSION['insert_ok'] = 0;
        }

        }

    When the page with the form loads for the first time, you may want to unset($_SESSION['insert_ok']) if it exists.
    Last edited by meth; 09-01-2007 at 04:02 AM. Reason: after thought
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • Users who have thanked meth for this post:

    fl00d (09-01-2007)

  • #3
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts
    ah thanks I hadn't thought of that.

    Thanks man


  •  

    Posting Permissions

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