In this article, Tim will show you how to create a quick-n-easy chat script using PHP and a very simple MySQL database. The script will allow visitors to choose a nickname, enter text to send to the chatting window, and view messages from other people... all in real-time! For a bit of fun, the chat application also has buttons to send several emote-icons including smiling and frowning faces.
A MySQL Driven Chat Script - The chat script explained (contd.) (Page 4 of 6 )
The GetInput function
This function is responsible for generating the bottom frame of our chat window, and allows the user to enter their message in a text box and send it. If you look at the screen shot shown above, you will also see that the bottom frame contains a “Say” button (to submit the message), a color drop-down list (this lets the visitor choose the color of their message), and several icon-icon buttons.
When we press enter in the text field, or click on the “Say” button, the JavaScript function “doSubmit” is called. This function will make sure that the visitor has actually entered a message. If they have, then the function will get the currently selected color from the drop-down list, and change the value of the text field so that it’s surrounded by <font> and </font> tags. The function will return true, and our form is submitted. If the text field is empty, the browser displays a message box kindly asking the user to enter a message and returns false, stopping the form from being submitted. The code for the “doSubmit” function is shown below:
When one of the icon-icon buttons is pressed, the form will call the sendFace JavaScript function, which will automatically send either “:)”, “:(”, or “:D” to the top frame of our chat window. When the top frame receives either of these, the ShowAddPosts function will automatically display an image corresponding to that icon-icon, instead of the “:)”. So, for example, if we entered “:)” in the text field or clicked on the “:)” button, the top frame would display the appropriate icon-icon image, like this:
The images for the icons-icons are included as part of the support material at the end of this article, and should be saved in the same directory as the chat.php script (which is also included as part of the support material).
The code for the entire GetInput function is shown below: