How do i make temporary variables disappear on page refresh?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

How do i make temporary variables disappear on page refresh?

Post by drayarms »

Hello folks, well what I mean by temporary variables are variables like error messages or success messages that appear after a form has been submitted. I want to be able to kill these variables when the page is refreshed and return the page to its defualt state. For example, I'm making this simple page which is basically a form that ask users for some information, then upon submit, stores the in a database and prints a success message. If the form is filled out incorrectly or some form fields are left blank, then error messages are printed. What I really want is for these messages to disappear when the page is refreshed. My current model uses a second page to handle the form, and then upon succes or upon encountering an error, redirects to the form page, passing the error or success variables via the url to the form page where they are printed out. What can I do differently to achieve what I want (ie kill variables upon page refresh)? Below are the form page, and the page that handles the form respectively.

Code: Select all


				<form method="post" action="send_email.php">


					<div id="contact_form">


						<div align="left" class="green18">Contact Us</div> <br/> <br/>


						<label>Your Name:</label> <input class="texta" name ="name" size="20"      maxlength="49"/> <br/> <br/>

						<div class ="error" style="position:relative;bottom:40px;left:128px" > 

							<?php //Retrieve the encoded errors string from send_email.php page

							$errors_string=$_GET['errors_string'];

							//Explode the decoded errors string back to an array.
                			
							$errors = explode(",", $errors_string);
							echo $errors[0];?>

						</div> 		

						<label>Your Company Name:</label> <input class="texta" name ="company" size="30" maxlength="66"/> <br/> <br/>


						<div class ="error" style="position:relative;bottom:40px;left:128px" > 

							<?php //Retrieve the encoded errors string from send_email.php page

							$errors_string=$_GET['errors_string'];

							//Explode the decoded errors string back to an array.
                			
							$errors = explode(",", $errors_string);
							echo $errors[1];?>

						</div> 		

						<label>Subject(Optional):</label> <input class="texta" name ="subject" size="20" maxlength="49"/>  <br/> <br/>


						<label>Email:</label> <input class="texta" name ="email" size="20" maxlength="49"/>  <br/> <br/>


						<div class ="error" style="position:relative;bottom:40px;left:128px" > 

							<?php //Retrieve the encoded errors string from send_email.php page

							$errors_string=$_GET['errors_string'];

							//Explode the decoded errors string back to an array.
                			
							$errors = explode(",", $errors_string);
							echo $errors[2];?>

						</div> 		


						<label>Message:</label> <textarea style="float:left" class="" name ="message" cols="40" rows="3"> </textarea> <br/> <br/>

						<div class ="error" style="position:relative;bottom:-19px;left:-260px" > 

							<?php //Retrieve the encoded errors string from send_email.php page

							$errors_string=$_GET['errors_string'];

							//Explode the decoded errors string back to an array.
                			
							$errors = explode(",", $errors_string);
							echo $errors[3];?>

						</div> 		
		

						<button class="button" type ="submit" name ="submit">Submit</button> <br/>

						<div id ="sent" > 

							<?php //Retrieve the user id from send_email.php page

							$sent=$_GET['sent'];

							$send_msg = urldecode($sent);  echo $send_msg;  ?>

						</div> <!--closes sent-->			
					

					</div>


				</form>
 



Code: Select all



<?php


//address error handling

ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);




if (isset($_POST['submit'])) {
 
    $errors = array();


// Connect to the database.

        require('config/config.php');


//Check for errors.


//Check to make sure they entered their name and it's of the right format.

if (eregi ("^([[:alpha:]]|-|')+$", $_POST['name'])) {

        $a = TRUE;

    } else {

        $a = FALSE;

        $errors[0] = '*Please enter a valid name.';


   }



//Check to make sure they entered their company name.

if (!empty ( $_POST['company'])) {

        $b = TRUE;

    } else {

        $b = FALSE;

        $errors[1] = '*Please enter company name.';


   }



//Check to make sure email is valid.

if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) { 
 

         $c = TRUE; 


}else { 

         $c = FALSE;

      $errors[2] = '*Please enter a valid email address.'; 

      } 




//Check to make sure they entered their message.

if (!empty ( $_POST['message'])) {

        $d = TRUE;

    } else {

        $d = FALSE;

        $errors[3] = '*Please enter your message.';


   }




 

//If no errors

if (empty($errors)) {

//Create variables for all the post .

 $name = $_POST['name'];

 $company = $_POST['company'];

 $email = $_POST['email'];

 $message = $_POST['message'];

 $ip = $_SERVER[REMOTE_ADDR];

 $date = (date("Y/m/d:G:i:s"));

 $timestamp = ( mktime(date(H), date(i), date(s), date(m) , date(d) , date(Y)));


//Formulate the insert query.

        $query = "INSERT INTO emails ( email_id, name, company, email, message, ip, date,timestamp) VALUES ( 0, '$name','$company','email', '$message', '$ip' , '$date', '$timestamp' )";
       
        $result = mysql_query($query) or die("Data could not be inserted into table because: " .mysql_error());



	if (mysql_affected_rows() == 1) {

    
        	// Display success message


       	 	$sent_msg =     "Your email has been sent.  We will get back to you shortly.";

		$sent = urlencode($sent_msg);  

					
        	// Display contact page

		header("Location: contact_page.php?sent=$sent");  exit();

	}else{die("There was a problem: " . mysql_error());}
            
			

//Display error messages.


} else {// if errors array is not empty

	//Confer the errors array into a string
	
	$errors_string = implode(",", $errors);

	//Encode the imploded string.
	
	$error_message = urlencode($errors_string);


        	// Display page

		header("Location: contact_page.php?errors_string=$errors_string");  exit();


	        
    }//End of if there are errors.


}//End of if submit.




?>


Also, another problem I just noticed is that, the errors[3] variable isn't getting passed back to the form page. I don't know if it's because the url can only pass so much info to a second page. The page in question can be located here:

http://creativewizz.com/contact_page.php
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do i make temporary variables disappear on page refr

Post by Celauran »

Might be easier and cleaner to store errors in a session variable to avoid cluttering up the URL and to avoid having to use explode() and wonder what's where. Also, speaking of the form, you shouldn't use inline styling.

$_POST is always set, so isset($_POST) is redundant. Also, eregi is deprecated. Use preg_ functions instead.
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

Re: How do i make temporary variables disappear on page refr

Post by drayarms »

points taken celauran, but the main problem still remians. how to unset the variables on page refresh.
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: How do i make temporary variables disappear on page refr

Post by maxx99 »

You can use http://php.net/manual/en/function.unset.php to unset your variable each time after you read it. So its not there on refresh :)
That's the general idea of Flash Messanger from Zend Framework

Remember not to use this one to unset entire session :)
Post Reply