...

View Full Version : Clearing variables after being submitted through a form



fl00d
09-01-2007, 03:29 AM
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
$blah = $_POST['blah'];
$food = $_POST['cookie'];
$sql = mysql_query...
if($sql){
//insert...
}
//clear variables
$blah = "";
$food = "";
?>

To check for blanks I implement this:

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

meth
09-01-2007, 03:59 AM
How about using a session?



if(!$_SESSION) session_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.

fl00d
09-01-2007, 04:30 AM
ah thanks I hadn't thought of that.

Thanks man :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum