Page 1 of 1

Need help

Posted: Tue Sep 27, 2005 7:17 am
by swede
I have created this simple shoutbox and have an problem with it.
When I write something it prints it out perfectly. But if I press reloadbutton on webbbrowser it writes the same message again. I think this could be because of the $_post but I dont know how to fix it.
The code:

Code: Select all

<?php 
 include "style.css";
 include "sqlconnect.php";
  
   $namn = $_POST['namn']; 
   $meddelande = $_POST['meddelande']; 
   $ip = $_POST['ip']; 
   $mlen = strlen($message); 
   $maxlength = 150; 
   $datum = date("M jS Y"); 
  
  
   if ($_POST['submit'])  { 
     
      if ($namn == "") {  
       echo "<strong>Error: Du glömde att fylla i ditt namn!</strong>"; 
     } 
     
     else if ($meddelande == "") { 
       echo "<strong>Fel-->Du har inte skrivit något meddelande.</strong>"; 
     } 
     
     else if ($mlen > $maxlength) { 
       echo "<strong>Fel-->Meddelandet är för långt.</strong>"; 
     } 
     else { 
       
       $db = mysql_connect($dbhost,$dbanv,$dblosen); 
       mysql_select_db($dbnamn) or die(mysql_error()); 
       mysql_query("INSERT INTO shoutbox(namn,meddelande,datum,ip) VALUES('$namn','$meddelande','$datum','$ip')"); 
     } 
   } 
   
   $db = mysql_connect($dbhost,$dbanv,$dblosen); 
   mysql_select_db($dbnamn) or die(mysql_error()); 
   $query = "SELECT * FROM shoutbox ORDER BY id DESC LIMIT 10"; 
   $result = mysql_query($query); 
   
 ?> 
 <div> 
   <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
     <strong>Ditt namn:</strong><br/> 
     <input type="text" name="namn" maxlength="20"><br/> 
     <strong>Meddelande:</strong><br/> 
     <textarea name="meddelande"></textarea><br/> 
     <input type="submit" name="submit" value="Shout It!"> 
     <input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>"> 
   </form> 
 </div>
 <?php 
   
   echo "<div id=\"contentbox\">\n"; 
   echo "<ul id=\"shoutboxmessage\">\n"; 
   while($r = mysql_fetch_array($result)) { 
     $namn = $r['namn']; 
     $namn = strip_tags($namn); 
     $meddelande = $r['meddelande']; 
     $meddelande = strip_tags($meddelande); 
     echo "<li><strong>$namn</strong>: $meddelande</li>\n"; 
   } 
   echo "</ul>\n"; 
   echo "</div>\n"; 
   mysql_close($db);
    
  
 ?>

Posted: Tue Sep 27, 2005 7:35 am
by shiznatix
ya when you hit refresh it is going to resubmit the form so it would put it in twice. the best way to fix this is just have it submit to a seperate page then have it header() back to the original

Posted: Tue Sep 27, 2005 7:36 am
by feyd
you have to use the submit button again, refreshing the page will only resubmit the last page's post data.

Posted: Tue Sep 27, 2005 11:00 am
by s.dot
You could send the forms action to a frame or iframe, then clear the text box using javascript. Then if you refreshed the page, a new instance of the shoutbox would load instead of submitting the $_POST data again.