Passing variable to php for chatbox

Passing variable to php for chatbox

Hello. I'm trying to pass a variable from jquery to PHP to write it to an HTML file that then gets read by PHP that contains the original jquery. (chatbox) I'm not quite sure where it's broken. I don't know jquery at all, I've just pulled someone else's code that they put out and modified it. 

This is the a.php which passes the variable to b.php


  1. <!--THIS IS WHERE THE CHAT STARTS-->

  2. <div id="wrapper">
  3. <div id="menu">
  4. <p class="welcome">Welcome, <b><?php echo $first_name; ?></b></p>
  5. <div style="clear:both"></div>
  6. </div>
  7. <div id="chatbox"><?php
  8. if(file_exists("log.html") && filesize("log.html") > 0){
  9. $handle = fopen("log.html", "r");
  10. $contents = fread($handle, filesize("log.html"));
  11. fclose($handle);
  12. echo $contents;
  13. }
  14. ?></div>
  15. <form name="message" action="">
  16. <input name="usermsg" type="text" id="usermsg" size="63" />
  17. <input name="submitmsg" type="submit"  id="submitmsg" value="Send" />
  18. </form>
  19. </div>
  20. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
  21. <script type="text/javascript">
  22. // jQuery Document
  23. $(document).ready(function(){
  24. //If user submits the form
  25. $("#submitmsg").submit(function(){
  26. var clientmsg = $("#usermsg").val();
  27. //$.post("post.php", {text: clientmsg});
  28. $.ajax(
  29. {
  30. url: "post.php",
  31. data: clientmsg,
  32. })
  33. $("#usermsg").attr("value", "");
  34. return false;
  35. });
  36. //Load the file containing the chat log
  37. function loadLog(){
  38. var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
  39. $.ajax({
  40. url: "log.html",
  41. cache: false,
  42. success: function(html){
  43. $("#chatbox").html(html); //Insert chat log into the #chatbox div
  44. var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
  45. if(newscrollHeight > oldscrollHeight){
  46. $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
  47. }
  48.   },
  49. });
  50. }
  51. setInterval (loadLog, 2500); //Reload file every 2.5 seconds
  52. //If user wants to end session
  53. $("#exit").click(function(){
  54. var exit = confirm("Are you sure you want to end the session?");
  55. if(exit==true){window.location = 'index.php?logout=true';}
  56. });
  57. });
  58. </script>
This is b.php

  1. <?php
  2. session_start();
  3. if ( $_SESSION['logged_in'] != 1 ) {
  4. $text = $_POST['text'];
  5. $fp = fopen("log.html", 'a');
  6. fwrite($fp, "<div class='msgln'>(".date("g:i A").") <b>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
  7. fclose($fp);
  8. }
  9. ?>