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
- <!--THIS IS WHERE THE CHAT STARTS-->
- <div id="wrapper">
- <div id="menu">
- <p class="welcome">Welcome, <b><?php echo $first_name; ?></b></p>
- <div style="clear:both"></div>
- </div>
- <div id="chatbox"><?php
- if(file_exists("log.html") && filesize("log.html") > 0){
- $handle = fopen("log.html", "r");
- $contents = fread($handle, filesize("log.html"));
- fclose($handle);
-
- echo $contents;
- }
- ?></div>
-
- <form name="message" action="">
- <input name="usermsg" type="text" id="usermsg" size="63" />
- <input name="submitmsg" type="submit" id="submitmsg" value="Send" />
- </form>
- </div>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
- <script type="text/javascript">
- // jQuery Document
- $(document).ready(function(){
- //If user submits the form
- $("#submitmsg").submit(function(){
- var clientmsg = $("#usermsg").val();
- //$.post("post.php", {text: clientmsg});
- $.ajax(
- {
- url: "post.php",
- data: clientmsg,
- })
- $("#usermsg").attr("value", "");
- return false;
- });
-
- //Load the file containing the chat log
- function loadLog(){
- var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
- $.ajax({
- url: "log.html",
- cache: false,
- success: function(html){
- $("#chatbox").html(html); //Insert chat log into the #chatbox div
- var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
- if(newscrollHeight > oldscrollHeight){
- $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
- }
- },
- });
- }
- setInterval (loadLog, 2500); //Reload file every 2.5 seconds
-
- //If user wants to end session
- $("#exit").click(function(){
- var exit = confirm("Are you sure you want to end the session?");
- if(exit==true){window.location = 'index.php?logout=true';}
- });
- });
- </script>
This is b.php
- <?php
- session_start();
- if ( $_SESSION['logged_in'] != 1 ) {
- $text = $_POST['text'];
- $fp = fopen("log.html", 'a');
- fwrite($fp, "<div class='msgln'>(".date("g:i A").") <b>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
- fclose($fp);
- }
- ?>