slpctrl Posted May 7, 2008 Share Posted May 7, 2008 <?php function clean ($var) { return htmlspecialchars($var); return stripslashes($var); } include("main.html"); $user=clean($_POST['nick']); $message=clean($_POST['text']); if(isset($user) && isset($message) && !empty($user) && !empty($message)) { $stuff="<font color=\"#00FF00\"><b>$user: </font></b><font color=\"#FFFFFF\">$message</font><br>\n"; $file=fopen("chat.html","a"); fwrite($file, $stuff); fclose($file); } else die(); ?> Here's my PHP code, the main.html isn't really important it just has an iframe with the chat HTML screen, and an input for a nick and message. But when you refresh it, it reposts. How do I prevent this from happening? Link to comment https://forums.phpfreaks.com/topic/104612-clearing-post-values-after-use/ Share on other sites More sharing options...
slpctrl Posted May 7, 2008 Author Share Posted May 7, 2008 Whoops, never mind I got it. Just added the HTML to the PHP: <html> <head> <title>Chat Box</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <center> <br><br><br><br><br><br><br><br><br> <H1>Chat Box</H1> <iframe src="chat.html" width="700" height="250"></iframe> <br> <br> <form name="input" action="chat.php" method="post" target="_self"> Nick:<br><input type="text" name="nick"><br> Message:<br><input type="text" name="text"><br><br> <input type="submit" name="submit" value="submit"> </form> </center> </html> <?php function clean ($var) { return htmlspecialchars($var); return stripslashes($var); } $user=clean($_POST['nick']); $message=clean($_POST['text']); if(isset($user) && isset($message) && !empty($user) && !empty($message)) { $stuff="<font color=\"#00FF00\"><b>$user: </font></b><font color=\"#FFFFFF\">$message</font><br>\n"; $file=fopen("chat.html","a"); fwrite($file, $stuff); fclose($file); header('location: ./chat.php'); } else die(); ?> Link to comment https://forums.phpfreaks.com/topic/104612-clearing-post-values-after-use/#findComment-535431 Share on other sites More sharing options...
slpctrl Posted May 7, 2008 Author Share Posted May 7, 2008 Wait, that didn't work. How do I make it so that you can't refresh and it will post the same thing again? Link to comment https://forums.phpfreaks.com/topic/104612-clearing-post-values-after-use/#findComment-535438 Share on other sites More sharing options...
rhodesa Posted May 7, 2008 Share Posted May 7, 2008 <?php function clean ($var) { return htmlspecialchars($var); return stripslashes($var); } $user=clean($_POST['nick']); $message=clean($_POST['text']); if(isset($user) && isset($message) && !empty($user) && !empty($message)) { $stuff="<font color=\"#00FF00\"><b>$user: </font></b><font color=\"#FFFFFF\">$message</font><br>\n"; $file=fopen("chat.html","a"); fwrite($file, $stuff); fclose($file); header('location: '.$_SERVER['REQUEST_URI']); exit; } ?> <html> <head> <title>Chat Box</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <center> <br><br><br><br><br><br><br><br><br> <H1>Chat Box</H1> <iframe src="chat.html" width="700" height="250"></iframe> <br> <br> <form name="input" method="post"> Nick:<br><input type="text" name="nick"><br> Message:<br><input type="text" name="text"><br><br> <input type="submit" name="submit" value="submit"> </form> </center> </html> Link to comment https://forums.phpfreaks.com/topic/104612-clearing-post-values-after-use/#findComment-535453 Share on other sites More sharing options...
Mr. Despair Posted May 7, 2008 Share Posted May 7, 2008 Newbie question: I was wondering if its generally advisable to use request_uri instead of php_self? I've been using the latter... Any examples where one would act differently than the other when using the header? Link to comment https://forums.phpfreaks.com/topic/104612-clearing-post-values-after-use/#findComment-535470 Share on other sites More sharing options...
rhodesa Posted May 7, 2008 Share Posted May 7, 2008 REQUEST_URI includes GET variables....so if this was the URL: http://www.example.com/somepage.php?id=123 PHP_SELF: /somepage.php REQUEST_URI: /somepage.php?id=123 ...both have their uses Link to comment https://forums.phpfreaks.com/topic/104612-clearing-post-values-after-use/#findComment-535473 Share on other sites More sharing options...
slpctrl Posted May 7, 2008 Author Share Posted May 7, 2008 Okay, I got all that. Now how do I take this: <html> <head> <title>Chat Box</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <center> <br><br><br><br><br><br><br><br><br> <H1>Chat Box</H1> <iframe src="chat.html" width="700" height="250"></iframe> <br> <br> <form name="input" action="chat.php" method="post" target="_self"> Nick:<br><input type="text" name="nick"><br> Message:<br><textarea name="text" cols="30" rows="5"></textarea><br><br> <input type="submit" name="submit" value="submit"> </form> </center> </html> <?php function clean ($var) { return htmlspecialchars($var); return stripslashes($var); } if($_POST['nick'] && $_POST['text']) { $user = clean($_POST['nick']); $message=clean($_POST['text']); $stuff="<font color=\"#00FF00\"><b>$user: </font></b><font color=\"#FFFFFF\">$message</font><br>\n"; $file=fopen("chat.html","a"); fwrite($file, $stuff); fclose($file); header('Location: chat.php'); } ?> And make the iframe scroll to the bottom??? Link to comment https://forums.phpfreaks.com/topic/104612-clearing-post-values-after-use/#findComment-535480 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.