Skip to main content

PHP/MySQLi Creating a Forum - Part 13 - User Page #3 - Sending Messages

Body
Introduction: This tutorial will be continuing my series of creating a forum in PHP/MySQLi/HTML. In this thirteenth part, We will be continuing our user page, more precisely, we will be adding a message form for the logged in user to contact the specified user. Pre-creation: First you will need a host for your PHP, either a web host or localhost is fine but you will need PHP and MySQL(i) capabilities. Also, this will not be covering creating users, or styling the pages. For the purpose of using the logged in users username, we will be using $_SESSION['username']; from my login script, you can find that tutorial on my profile page. Obviously you will also need to go through the first, second, third, fourth, fifth, sixth, seventh, eight, ninth, tenth, eleventh and twelfth parts of this tutorial series which can all be found on my profile tracking page. User Page Editing: First we need the HTML form for the contact function...
  1. <br/>
  2. <h1>Contact</h1>
  3. <form <?php echo 'action="userPage.php?username='.$user.'"'; ?> method='POST'>
  4. <table>
  5. <tbody>
  6. <tr>
  7. <td>Message:</td><td><input type='text' name='message' /></td>
  8. </tr>
  9. <tr>
  10. <td></td><td><input type='submit' value='Send Message' name='messageSent' /></td>
  11. </tr>
  12. </tbody>
  13. </table>
  14. </form>
PHP: Now for the actual sending PHP function/code. First we want to check if the messageSent post variable is set (that the submit button has been pushed), then we want to check and validate the title and message. If they are ok, we will add the information to our messages table within our forumTutorial database (created in the previous tutorial)...
  1. if (isSet($_POST['messageSent']) && isSet($_POST['message']) && $_POST['message'] != '') {
  2. $message = $_POST['message'];
  3. $from = $_SESSION['username'];
  4. $to = $_GET['username'];
  5. $q = mysqli_query($con, "INSERT INTO `messages` VALUES ('', '$from', '$message', '$to')");
  6. if ($q) {
  7. echo 'Message sent.';
  8. }else
  9. echo 'Failed to send message, contact the site administrator.';
  10. }
Next Tutorial: In the next tutorial, we will go over receiving messages.

Comments

Add new comment