Jump to content

[SOLVED] how to stop my shoutbox posting after i refresh page?


Robert Elsdon

Recommended Posts

hello, i hope yous can help me, Ok what it is, is when i post a comment on my shoutbox, it posts and i can see it, but when i press refresh it posts again, i want to prevent this from happning. any ideas? please help! thanks.

 

<?PHP session_start(); ?>

<?
// Your database connection commands
mysql_connect("localhost","root","12345678");
mysql_select_db("shoutbox");

if($_POST['submit']) { ?>

<?
$time=date("h:ia d/m/y");

$insertshout="INSERT INTO shoutbox(name,message,time) VALUES ('$name', '$message','$time')";
mysql_query($insertshout) or die("Cannot insert shout");
?>

<? } else { } ?>


<?
// Get the messages from the databse
$get_messages = mysql_query("select * from shoutbox");
$amountofmessages = mysql_num_rows($get_messages);

$select_shouts = mysql_query("select * from shoutbox ORDER BY id DESC LIMIT 10");
while($r=mysql_fetch_array($select_shouts)) { ?>	


<font color="#b2eb62"><?=$r["time"];?></font> <b><font color="#b2eb62"><?=$r["id"];?></font></b>

<br>
   

<b><i><?=$r["name"];?></i></b><font color="#b2eb62"> : </font><?=$r['message'];?><br>

<? } ?>

Anytime you press refresh any variables sent will be resent to the browser therefore causing it to resubmit and duplicate your post.

 

This is just standard browser behavior, I suggest adding text to your page that says do not refresh the page after a submission.

i fixed it..

 

<?PHP session_start(); ?>

<?
// Your database connection commands
mysql_connect("localhost","root","12345678");
mysql_select_db("shoutbox");

if($_POST['submit']) { ?>

<?
$name	= $_POST['name'];
$message= $_POST['message'];
$time=date("h:ia d/m/y");

$insertshout="INSERT INTO shoutbox(name,message,time) VALUES ('$name', '$message','$time')";
mysql_query($insertshout) or die("Cannot insert shout");
?>
[b]<script type="text/JavaScript">window.location='index.php';</script>[/b]
<? } else { } ?>


<?
// Get the messages from the databse
$get_messages = mysql_query("select * from shoutbox");
$amountofmessages = mysql_num_rows($get_messages);

$select_shouts = mysql_query("select * from shoutbox ORDER BY id DESC LIMIT 10");
while($r=mysql_fetch_array($select_shouts)) { ?>	


<?=$r["time"];?> <b><font color="#b2eb62"><?=$r["id"];?></font></b>

<br>
   

<b><?=$r["name"];?></b><font color="#b2eb62"> : </font><?=$r['message'];?><br>

<? } ?>

okay a little slow but

i wrote a quick example of the using a token

<?php
session_start();
if(!empty($_POST['data']))
{
if($_SESSION['token'] == $_POST['token'])
{
	echo "User submitted: ".$_POST['data'];
}else{
	echo "Error: invalid Token";
}
}
$_SESSION['token'] = uniqid("token",true);
?>
<form action="" method="post">
<input name="token" type="hidden" value="<?php echo $_SESSION['token'];?>" />
<textarea name="data" cols="50" rows="3"></textarea><br />
<input name="submit" type="submit" value="submit" />
</form>

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.