Download
live chat room application n.
Skip this Video
Loading SlideShow in 5 Seconds..
Live Chat Room Application PowerPoint Presentation
Download Presentation
Live Chat Room Application

Live Chat Room Application

129 Vues Download Presentation
Télécharger la présentation

Live Chat Room Application

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Live Chat Room Application

  2. Create MySQL Table • Table: chat • Field 1: id • Data type: INT AUTO_INCREMENT, • Field 2: chattime • Data type: DATETIME, • Field 3: nick • Data type: CHAR (50), • Field 4: message • Data type: TEXT • Primary key: id

  3. Create MySQL Table • Table: chat • Field 1: id • This field is used to become the primary key for this table • Field 2: chattime • This field is used to store the time a chat message is posted • Field 3: nick • This field is used to store the nick name of the chatter • Field 4: message • This field is used to store the chat messages

  4. Create MySQL Table CREATE TABLE chat( id int(5) auto_increment, chattime datetime, nick char(50), message text, PRIMARY KEY (id) );

  5. Create HTML form to allow the user to enter his/her nickname • Create a form (call it login.html) to allow the user to enter his/her nickname • A form with one textbox (call it “nick”) and one submit button • When the submit button is clicked, the next page (call it “main.php”) must be opened

  6. Create HTML form to allow the user to enter his/her nickname • login.html <html> <font size = "+2"> <B> Enter your nickname:</B> </font> <p> <form method="post" action="main.php“> <input type="text" name="nick" cols="50“> <p> <input type="submit" value="Login/Chat“> </form> </html>

  7. main.php • After the user enters the nickname and clicks the submit button, the “main.php” must be displayed • This page contains php code to enter the nickname into a cookie variable to be used in other pages • This page also displaystwo frames: • Frame 1 displays the messages and • Frame 2 displays the textbox and the submit button to allow the user to enter a new message

  8. main.php <?PHP //Retrieve the nickname from the nick textbox $a = $_POST["...."]; //Create a cookie variable to store the nickname //Why do we need to do it? //Because we need to use the nickname in the other application: speak.php setcookie('....' , .... ) ; //Use HTML code to break the page into two frames //Use the first frame to display the messages. The source of this frame is display.php //Use the second frame to display the textbox and the submit button //to enter a new message. The source of this frame is speak.php ?> <html> <frameset rows="80%,*“> <frame src=" .... " name="chatdisplay“> <frame src=“ .... " name="speak“> </frameset> </html>

  9. main.php • “main.php” contains two frames: • “display.php” • To repeatedly(every second) read the “chattime”, “nickname”, and “messages” from the “chat” table, and • To display this information on the screen • “speak.php” • To allow the user to submit a new message • After the user clicks the submit button, retrieve the nickname from the cookie variable and the message from the textbox. Then, insert the chattime (use date()), nickname, and message into the chat table

  10. How To Refresh a Webpage Automatically? <META HTTP-EQUIV="Refresh" CONTENT=“3 ; URL=http://www.some.com/some.html"> • CONTENT=“3 • Refresh the page every 3 seconds • URL=http://www.some.org/some.html” • The address of the Webpage that is refreshed every 3 seconds

  11. display.php <html> <head> <!-- Execute the PHP code (that reads the messages from the database) in this file every 0.0001 second --> <meta http-equiv="refresh" content="0.0001;url=display.php"> </head> <body> <?PHP $conn=mysql_connect(“...",”....",“...."); mysql_select_db(“....",$conn); //creating the mysql commands to read all records from the chat table //sorted in ascending order by chattime field $sql=“...." ; //execute the mysql commands $result = .... ; //put the records in an array, read each record, and display it while ( $arrline = .... ) { //Read each record and putting it in an array print "<i>" ; print .... . " " ; //Display the first field of the record print .... . " says : </i><b>" ; //Display the second field of the record print ... . "</b><br>" ; //Display the third field of the record } ?> </body> </html>

  12. speak.php <html> <!-- Create the textbox and the submit button to allow the user to enter a new message --> <form method = “POST”> <input type="text" name="message" cols="500"> <input type="submit" value="Speak"> </form> </html> <?PHP If ($_REQUEST["message"]) { //if user has typed a message in the messages textbox $nick = $_COOKIE["nick"]; //retrieve the nickname from the nick cookie variable $message = $_POST["message"]; //retrieve the message from the messages textbox $conn=mysql_connect(“...", "....", "...."); mysql_select_db(“....",$conn); //create mysql command to add the chattime, user nickname, and messages to the chat table $sql=" .... " ; //execute the mysql command .... } ?>