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

Link to comment
Share on other sites

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.';
}
?>

Link to comment
Share on other sites

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*

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.