Jump to content

refreshing page.. postdata


logicopinion

Recommended Posts

hello again..

 

i need some advice about problem described below.

 

so:

 

i have form where i enter some text

 

here is the form

 

<form action="index.php?p=itm1" method="post">
<table height="100" border="0" cellpadding="0" cellspacing="0">
<TR><TD colspan="3"><i>some text</i></TD></TR>
<TR>
<TD align="center">enter new name : </TD>
<TD align="center"><input type="text" name="item"></TD>
<TD align="center"><input type="submit" value="add"></TD>
</TR>
</table>
</form>

 

after this text inserted into form as u see is sent to php file called itm1.php

 

here is the code of that page

 

 

 

<?php
include("includes/vars.php");

$item = $_POST['item']; 

if ($_POST['item']=$item)

{

mysql_connect("$hostname",  "$username",  "$password") or die(mysql_error());
mysql_select_db("$db") or die(mysql_error());
mysql_query("CREATE TABLE IF NOT EXISTS menu(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
item VARCHAR(300)) ") or die(mysql_error());
mysql_query("INSERT INTO menu (item) 
VALUES('$item')") or die(mysql_error()); 


print "<br />";
echo "  new item named <b>$item</b> has been added";
print "<br />";
}

else

{

echo "error.. error description";

}


?>

 

 

if "if" statmant is true it adds new item to db and echos "new item has been added"  so everything goes fine.. but

 

when i refresh the page post data is added to database again..

 

how can i avoid this? i want if someone refreshes the page ... to do something else but not to add the same item again to database.

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/77458-refreshing-page-postdata/
Share on other sites

Hi.

 

First of all, there is error in your script ;)

Not

<?php
if ($_POST['item']=$item) //.....
?>

but

<?php
if ($_POST['item']==$item) //.....
?>

 

And now your problem. You could use header('Location: some_url'); to reload page. After reloading page all your post variables will be deleted, so they wont be inserted in database again.

When you refresh the script page, the browser (but maybe not all) WILL send the post data again. To solve your problem, you could header-redirect the user back to the form page (or just to the same script page like kopytko suggests). If the former, then you could print your 'success' message using $_GET. For example, use this instead of printing the messages:

 

<?php
header('Location: formpage.php?ok=true'); //success
?>

<?php
header('Location: formpage.php?ok=false'); //error
?>

 

To display a success/error message in you form page, use:

 

<?php
if ($_GET['ok'] == 'true') {
echo 'Added to database.';
} elseif ($_GET['ok'] == 'false') {
echo 'Error.';
}
?>

Following on from kopytko, i noted the following :

$item = $_POST['item']; 

 

So when you do your if test

if ($_POST['item'] == $item)

 

It will ALWAYS be true, because you just made a = b and then tested to see if a and b are the same... of course they are... DOH! *slaps forehead*

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.