Problem: conecting dinamic page and chat

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
laponac84
Forum Newbie
Posts: 2
Joined: Mon Oct 04, 2010 11:59 am

Problem: conecting dinamic page and chat

Post by laponac84 »

Chat have: chat/ "index.php", "post.php", "style.css" and "log.html"

Script of chat/ "index.php", "post.php" i listed below, log.html conatin message from chat.

original chat works OK. but when connecting to the site, i have problem....

I was able to connect users with chat an site and to read the last entered message until I connected chat with the site. but now i fail write new message from chat to "log.html" in chat folder

Wer is problem for input message from chat to log.html ?

link in menu for dinamic content part is

Code: Select all

<li><a href="index.php?section=chat&page=index" title="Chat"><span>Chat</span></a></li>

file: index.php

Code: Select all

<?php
session_start();

if(isset($_GET['logout'])){	
	
	//Simple exit message
	$fp = fopen("chat/log.html", 'a');
	fwrite($fp, "<div class='msgln'><i>User ". $_SESSION['user'] ." has left the chat session.</i><br></div>");
	fclose($fp);
	
	session_destroy();
	header("Location: index.php"); //Redirect the user
}

function loginForm(){
	echo'
	<div id="loginform">
	<form action="" method="post">
		<p>Please enter your name to continue:</p>
		<label for="user">Name:</label>
		<input type="text" name="user" id="user" />
		<input type="submit" name="enter" id="enter" value="Enter" />
	</form>
	</div>
	';
}

if(isset($_POST['enter'])){
	if($_POST['user'] != ""){
		$_SESSION['user'] = stripslashes(htmlspecialchars($_POST['user']));
	}
	else{
		echo '<span class="error">Please type in a name</span>';
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chat</title>
<link type="text/css" rel="stylesheet" href="chat/style.css" />
</head>

<?php
if(!isset($_SESSION['user'])){
	echo "Da bi ste se dopisivali na catu, morate biti prijavljeni";
}
else{
?>
<div id="wrapper">
	<div id="menu">
		<p class="welcome">Dobrodosli, <b><?php echo $_SESSION['user']; ?></b></p>
		<p class="logout"><a id="exit" href="#">Napusti Chat</a></p>
		<div style="clear:both"></div>
	</div>	
	<div id="chatbox">
        <?php
	if(file_exists("chat/log.html") && filesize("chat/log.html") > 0){
		$handle = fopen("chat/log.html", "r");
                
		$contents = fread($handle, filesize("chat/log.html"));
		fclose($handle);
		
		echo $contents;
	}
        else
           {
            echo "Fajl ne postoji";
           }
	?></div>
	
	<form name="message" action="index.php?section=chat&page=post">
		<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").click(function(){	
		var clientmsg = $("#usermsg").val();
		$.post("chat/post.php", {text: clientmsg});				
		$("#usermsg").attr("value", "");
		return false;
	});
	
	//Load the file containing the chat log
	function loadLog(){		
		var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
		$.ajax({
			url: "chat/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>
<?php
}
?>
</body>
</html>
file: post.php

Code: Select all

<?
session_start();
if(isset($_SESSION['user'])){
	$text = $_POST['text'];
	
	$fp = fopen("chat/log.html", 'a');
        fwrite($fp, "<div class='msgln'>(".date("g:i A").") <b>".$_SESSION['user']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
	fclose($fp);
}
?>
thanks :)
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Problem: conecting dinamic page and chat

Post by Jonah Bron »

Can you clarify the problem? Are you saying that it only loads the log file once and then stops?
laponac84
Forum Newbie
Posts: 2
Joined: Mon Oct 04, 2010 11:59 am

Re: Problem: conecting dinamic page and chat

Post by laponac84 »

In original chat code, i load "log.html" all time (for reading and writing)

but in main dinamic page (site page wer i show "chat/index.php") an start i show old message, but canot write new message in "chat/log.html"

in picture below you can see chat after click chat option in meny (sorry an bad english)
Attachments
Untitled-2.png
Untitled-2.png (175.28 KiB) Viewed 80 times
Post Reply