Jump to content

chat in php


visitor

Recommended Posts

Hi Guys

 

Somehow I can't make this chat work... maybe you've got an idea what the problem is?

 

main.php

 

<?php
session_start();
?>
<html>
  <head>
   <title>Chat</title>
    <script language="JavaScript">
    msg = new Array();
    function showchat()
    {
       for (i = 0; i < msg.length; i++)
       {
          show.document.write(msg[i];
          show.document.write("<br />");
       }
       show.document.write("<a name='end'></a>");
    }
    </script>
   </head>
    <frameset rows="0,*,45" border="0">
     <frame name="main" src="main.php">
     <frame name="show" src="show.php">
     <frame name="send" src="send.php" scrolling="no">
    </frameset>
   </html>
   
<?php
   session_start();
   $time = $_SESSION['time'];
   if (!isset($time)) $time = 0;
   $hdlDB = mysql_connect('localhost', 'root', '');
   $strSQL = "SELECT * FROM chat WHERE time > '$curTime'";
   $dhlRS = mysql_db_query('chat', $strSQL, $hdlDB);
   while ($arrRS = mysql_fetch_assoc($hdlRS))
   {
       $arrMsg[] = '<b>' . $arrRS['sender'] . '</b>: '
                         . $arrRS['data'];
       $time = max($arrRS['time'], $time);
   }
   session_register('time');
   
?>
<html>
  <head>
   <title>Chat</title>
    <script language="JavaScript">
     <?php
       if (is_array($arrMsg))
       {
          $i = 0;
          foreach($arrMsg as $strLine)
          {
             echo "top.msg[$i] = '$strLine';\n";
             $I++;
          }
       }
       ?>
       top.show.location.reload();
       </script>
      </head>
       <body onload="window.setInterval('location.reload()', 4000)">
      </body>
     </html>

 

 

send.php

 

<?php
session_start();
if (!isset($time)) $time = 0;
if (strlen($_POST['newmsg'] > 2)
{
   $hdlDB = mysql_connect('localhost', 'root'; '');
   $curTime = time();
   $strSQL = "INSERT INTO chat (time, data, sender)
              VALUES ('$curTime', $newmsg', '$sender')";
    mysql_db_query('chat', $strSQL, $hdlDB);
   session_register('time');
}  /* end if */
?>
<html>
<head>
  <title>Chat</title>
   <script language="JavaScript">
    <?php
    if (is_array($arrMsg))
    {
       foreach($arrMsg as $strLine)
          echo "top.msg[top.msg.length]=\"$strLine\"";
    }
    ?>
    top.show.location.reload();
    </script>
   </head>
    <body bgcolor="white">
     <form action="<?php echo $PHP_SELF; ?>" method="post">
     <select name="sender" size="1">
      <option value="Admin" <?php $sender == 'Admin' ? print 'selected' : print ''; ?>>Admin
      <option value="Client" <?php $sender == 'Client' ? print 'selected' : print ''; ?>>Client
     </select>
     's Beitrag:
     <input type="Text" name="newmsg" size="40">
     <input type="Submit" value="Send">
    </form>
   </body>
  </html>

 

 

show.php

 

<html>
<head>
  <title>Chat</title>
</head>
  <body>
   <script language="JavaScript">
      top.showchat();
   </script>
  </body>
</html>

 

 

Database (chat)

 

CREATE TABLE chat (

      id bigint(20) NOT NULL auto_increment,

      time varchar(10) NOT NULL,

    data text,

    sender varchar(32) DEFAULT '1' NOT NULL,

    PRIMARY KEY (id),

    KEY id (id)

);

 

 

cheers,

 

ozzo

Link to comment
Share on other sites

You're missing a closing bracket on line 4

 

if (strlen($_POST['newmsg'] > 2)

 

should be

 

if (strlen($_POST['newmsg'] > 2))

 

Syntax errors are usually triggered by something on the previous line. Easy problem to fix when you take a look at the code! Also a good text editor would pick this up for you.

Link to comment
Share on other sites

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.