logicopinion Posted November 15, 2007 Share Posted November 15, 2007 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 Quote Link to comment Share on other sites More sharing options...
kopytko Posted November 15, 2007 Share Posted November 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted November 15, 2007 Share Posted November 15, 2007 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.'; } ?> Quote Link to comment Share on other sites More sharing options...
aschk Posted November 15, 2007 Share Posted November 15, 2007 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* Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 15, 2007 Share Posted November 15, 2007 Change those above code with $_POST to the following: if(isset($_POST['item']) && !empty($_POST['item'])){ That checks to see if the user posted data, and that the data is not empty/null Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.