Jump to content

Need some help with shoutbox code?


proud

Recommended Posts

I'm trying to design a shoutbox for my site...

This shoutbox code works good but recently i added to it a (meta tag) because i want the page to referesh every five seconds and get the new messages from the database...

My problem is that the meta-tag makes the entire page refresh including the form in which i'm supposed to enter the message, so my objective is to let the part of the page which contains the form to be excluded from refreshing so how can i do that? and without needing to divide the code into two different files?

 

 

 

<?php
include( 'connect.php' );


  $name = $_POST['name'];
  $message = $_POST['message'];
  $ip = $_POST['ip'];
  $mlen = strlen($message);
  $maxlength = 150;
  $date = date("M jS Y");

  if ($_POST['submit'])  {
    if ($name == "") { 
      echo "<strong>Error: Please enter your nickname.</strong>"; 
    }
    else if ($message == "") { 
      echo "<strong>Error: No message to be sent.</strong>"; 
    }
    else if ($mlen > $maxlength) { 
      echo "<strong>Error: Message too long.</strong>"; 
    }
    else {
     
      mysql_query("INSERT INTO shoutbox(name,message,date,ip) VALUES('$name','$message','$date','$ip')"); 
    }
  }
?>
<html>

<head>

<title>Display Messages</title>

//////////<meta http-equiv="refresh" content="5;url=shoutbox.php">/////////////

</head>

<body>
<?
  $query = "SELECT * FROM shoutbox ORDER BY id DESC LIMIT 20"; 
  $result = mysql_query($query);

  echo "<div id=\"contentbox\">\n";
  echo "<ul id=\"shoutboxmessage\">\n";
  while($r = mysql_fetch_array($result)) {
    $name = $r['name'];
    $name = strip_tags($name);
    $message = $r['message'];
    $message = strip_tags($message);
    echo "<li><strong>>$name</strong>: $message</li>\n";
  }
  echo "</ul>\n";
  echo "</div>\n";

  mysql_close($conn);

?>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <strong>Nickname:</strong><br/>
    <input type="text" name="name" maxlength="20"><br/>
    <strong>Message:</strong><br/>
    <textarea name="message"></textarea><br/>
    <input type="submit" name="submit" value="Shout It!">
    <input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>">
  </form>
</div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/40245-need-some-help-with-shoutbox-code/
Share on other sites

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.