Need help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
swede
Forum Newbie
Posts: 3
Joined: Thu Sep 22, 2005 11:12 am

Need help

Post 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);
    
  
 ?>
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you have to use the submit button again, refreshing the page will only resubmit the last page's post data.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply