Jump to content

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>

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.