Current location: Hot Scripts Forums » Programming Languages » PHP » Php Clear Form Variables


Php Clear Form Variables

Reply
  #1 (permalink)  
Old 06-25-04, 01:53 PM
Grevus Grevus is offline
New Member
 
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Php Clear Form Variables

I have been doing my head in trying to set up a Guest Book.

I am only new to PHP

I have a Form which posts to a MySQL data base.
When a user fills in the form and clicks submit, it works and adds the message to the data base and it refreshes the page with the new post.

The problem is if the user comes back to the page with out clearing their internet cache the page is obviously old, but when they click refresh it says " The page can not be refreshed with out re-submitting info....and asks them to click retry" You know the popup message. If they click retry then thier message re-posts and there is two duplicates.

If you send a message and refresh five time then you will have five instances of the same post.

I need a way to clear the variables so it does not post again. I have even tried setting one the variables to a preset value directly after it uploads to the database, to try using an if statement to not let it post again.

eg

variable = users_value
submit
if variable <> pre_set_value
insert variable into database
variable = pre_set_variable

but the only way I can get the variables to change is to submit it via the form.

i have tried unset, session_destroy and a few other things that I read about but still nothing. Quite possible I am doing it wrong though.

ANY HELP WOULD BE APPRICATED

THANKS IN ADVANCE

Grevus
Reply With Quote
  #2 (permalink)  
Old 06-25-04, 02:15 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
what I do in similiar situation is redirect to the same page after you insert the data to database ..
something like:
PHP Code:

$insert mysql_query("INSERT INTO table_1 VALUES ('stuff')");


//this where you redirct to the same page
header("Location: $_SERVER[PHP_SELF]?$_SERVER[QUERY_STRING]"); 
however to use this, there should NOT be any output before the header() !
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 06-26-04, 11:15 AM
Grevus Grevus is offline
New Member
 
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the quick reply NEVERMIND but as I said I am new to php and I couldn't get it working it still Reposts when the user clicks Refresh. The active cut down code below is at:

www.mnmnt.id.au/test/test.html

the real guest book is at :

www.mnmnt.id.au/BFVPB/BFVPB.html

Following is a cut down version of the code, I have put remarks where I tried things I also tried

header("Location: $_SERVER[PHP_SELF]?$_SERVER[QUERY_STRING]"); :
after the include statement from my html

THANKS AGAIN


<?

if ( isset( $test_data ) && $test_data <> "already posted")

//

{
// check user input here!
$dberror = "";
$ret = add_to_database( $test_data, $dberror );
if ( ! $ret )
print "Error: $dberror<BR>";
else
display();
print "<center>Thank's for your feedback<br><br></center>";


// I tried setting $test_data to "already posted" then using adding an if statement under the isset function

//$test_data = "already posted"

// unset(test_data)
// unset($globals['test_data'])
// session_destroy()

write_form();
}

else {
display();
write_form();
}


function add_to_database( $test_data, &$dberror )

{
$user = "MY USER NAME";
$pass = "MY PASSWORD";
$db = "MY DATABASE";
$link = mysql_pconnect( "localhost", $user, $pass );
if ( ! $link )
{
$dberror = "Couldn't connect to MySQL server";
return false;
}

if ( ! mysql_select_db( $db, $link ) )
{
$dberror = mysql_error();
return false;
}

$query = "INSERT INTO test ( t1 )
values( '$test_data' )";

// header("Location: $_SERVER[PHP_SELF]?$_SERVER[QUERY_STRING]");
// unset(test_data)
// unset($globals['test_data'])
// session_destroy()


if ( ! mysql_query( $query, $link ) )
{
$dberror = mysql_error();
return false;
}
return true;
}


function write_form()
{
print "<center><table width=30%><tr><td width=100% height=\"50\">";

global $PHP_SELF;

print "<form action=\"$PHP_SELF\" method=\"POST\">\n";
print "Test data\n";
print "<input type=\"text\" name=\"test_data\">\n";
print "<br>";
print "<input type=\"submit\" value=\"submit!\">\n</form>\n";
print "</td></tr></table></center>";

}


function display()
{

$user = "MY USERNAME";
$pass = "MY PASSWORD";
$db = "MY DATABASE";
$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
die( "Couldn't connect to MySQL" );
mysql_select_db( $db, $link )
or die ( "Couldn't open $db: ".mysql_error() );
$result = mysql_query( "SELECT * FROM test" );
$num_rows = mysql_num_rows( $result );

print "<center>\n";

while ( $a_row = mysql_fetch_row( $result ) )
{
print "<table border=1 width=70%>\n";
print "<tr><td>\n";
print "$a_row[0]";
print "</td></tr>\n";
print "</table>\n";
print "<br>\n";
}
print "</center>\n";
mysql_close( $link );
}
?>

Last edited by Grevus; 06-26-04 at 11:38 AM.
Reply With Quote
  #4 (permalink)  
Old 06-26-04, 11:38 AM
landboy landboy is offline
Newbie Coder
 
Join Date: May 2004
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Try changing

if ( isset( $test_data ) && $test_data <> "already posted")

to

if ( isset( $test_data ) && $test_data == "already posted")

I don't know if that will work but try it.
<> is a variable which means Not Equal
TRUE if $a is not equal to $b.

Also
PHP Code:

{

// check user input here!
$dberror "";
$ret add_to_database$test_data$dberror );
if ( ! 
$ret )
print 
"Error: $dberror<BR>";
else
display();
print 
"<center>Thank's for your feedback<br><br></center>"
Where are the }'s and {'s for else??

You need to clean up your code. And try to tame it down with the functions and just try normal php.


Also you can set a cookie after they posted and if they try to repost display a error. That would work.

Last edited by landboy; 06-26-04 at 11:48 AM.
Reply With Quote
  #5 (permalink)  
Old 06-26-04, 12:46 PM
Grevus Grevus is offline
New Member
 
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
THANKS FOR THE QUICK REPLY LANDBOY ...


if ( isset( $test_data ) && $test_data <> "already posted")

was correct but i changed it to

if ( isset( $test_data ) && $test_data != "already posted")

if the variable is set and does not contain "already posted" then post it.

The problem is i cant get the variable to change to "already posted"

What would work is a command (or function) that would remove all variable data from that page ie unset() of some sort but that didn't work either.
Or even a way to clear the internet cache for that page.

If I drop a cookie is that gonna give an error when they click "refresh"? I'll read up about cookies.
I want them to be able to post as many messages as they want but I just dont want it to re-post the same message when they try to refresh the listing.

Here is the last code I tried. I have cleaned it up a bit. (WELL I DID MAKE IT BETTER BUT WHEN I SUBMITTED THIS POST IT REMOVED ALL THE TABS - IF YOU CLICK REPLY YOU WILL SEE IT SET OUT) Will try with-out functions just straight PHP tomorrow (later today actually) i need some sleep. ::

Thanks for all your help


<?

if ( isset( $test_data ) && $test_data != "already posted" )

{

// check user input here!

$dberror = "";
$ret = add_to_database( $test_data, $dberror );


if ( ! $ret )

print "Error: $dberror<BR>";

else {

display();
print "<center>Thank's for your feedback<br><br></center>";
global $test_data;
$test_data = "already posted";
write_form();
}
}

else {
display();
write_form();
}


function add_to_database( $test_data, &$dberror )
{
$user = "MY USERNAME";
$pass = "MY PASSWORD";
$db = "MY DATABASE";
$link = mysql_pconnect( "localhost", $user, $pass );

if ( ! $link )
{
$dberror = "Couldn't connect to MySQL server";
return false;
}

if ( ! mysql_select_db( $db, $link ) )
{
$dberror = mysql_error();
return false;
}

$query = "INSERT INTO test ( t1 )
values( '$test_data' )";


if ( ! mysql_query( $query, $link ) )
{
$dberror = mysql_error();
return false;
}
return true;
}


function write_form()
{
print "<center><table width=30%><tr><td width=100% height=\"50\">";
global $PHP_SELF;

print "<form action=\"$PHP_SELF\" method=\"POST\">\n";
print "Test data\n";
print "<input type=\"text\" name=\"test_data\">\n";
print "<br>";
print "<input type=\"submit\" value=\"submit!\">\n</form>\n";
print "</td></tr></table></center>";
}


function display()
{

$user = "MY USERNAME";
$pass = "MY PASSWORD";
$db = "MY DATABASE";


$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
die( "Couldn't connect to MySQL" );
mysql_select_db( $db, $link )
or die ( "Couldn't open $db: ".mysql_error() );
$result = mysql_query( "SELECT * FROM test" );
$num_rows = mysql_num_rows( $result );

print "<center>\n";

while ( $a_row = mysql_fetch_row( $result ) )
{
print "<table border=1 width=70%>\n";
print "<tr><td>\n";
print "$a_row[0]";
print "</td></tr>\n";
print "</table>\n";
print "<br>\n";
}
print "</center>\n";
mysql_close( $link );
}
?>

Last edited by Grevus; 06-26-04 at 01:06 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
formmail problem gscraper Perl 12 08-27-04 03:06 AM
Form -> text file -> php variables Bonzo PHP 1 06-16-04 07:37 AM
Can anyone help me ? (problem using php variables in html db insert code) chronic_ PHP 2 06-13-04 11:19 AM
Writing from form to php config file Squid44th PHP 1 04-22-04 02:16 PM
PHP Triad/Upload form eddyvlad PHP 6 10-06-03 11:17 PM


All times are GMT -5. The time now is 05:06 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.