Jump to content

Using PHP and jQuery to create a chat script


Imoddedu

Recommended Posts

Hey! Everything works except for writing to the log. What do you think is the problem? userform.php

<?phpsession_start();?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><title>Live Support v0.1.1</title></head><body><center><h1>Live Chat Support v0.2</h1><br /><h6>A * denotes a required feld!</h6></center><script language="JavaScript"><!--function valid(form) {	var customerName = form.custName;	var customerQuestion = form.usrQuestion;	if (customerName.value.length == 0 && customerQuestion.value.length == 0) {		alert("Please fill out the field for your name and question!");		return false;	} else if (customerName.value.length == 0) {		alert("Please fill out the field for your name!");		return false;	} else if (customerQuestion.value.length == 0) {		alert("Please fill out the field for your question!");		return false;	} else {		alert("Chat request accepted, forwarding you to an operator!");		return true;	}}// --></script><center><table border="0"><form method="post" action="chat.php" onSubmit="return valid(this)"><tr name="nameRow"><td><b><i>Name: *</i></b></td><td><input type="text" size="18" maxlength="18" name="custName" /> <br /></td></tr><tr name="probCatRow"><td><b><i>Type of Problem:</i></b></td><td><select name="probType"><option value="Affiliate">Affiliate</option><option value="cPanel">cPanel</option><option value="Database">Database Issues</option><option value="Domain">Domain Issues</option><option value="EMail">Email</option><option value="Account">Account</option><option value="Features">Features</option><option value="Other">Other</option></select><br /></td></tr><tr name="usrQuestionRow"><td><b><i>Question: *</i></b></td><td><textarea rows="10" cols="50" name="usrQuestion" wrap="physical">What is your question?</textarea><br /></td></tr></table></center><center><input type="submit" value="Start Chart" name="startChat" /></center></form><br /><br /><center><a href="patchlog.txt">Patch Log</a> | <a href="login.php">Operator Login</a> | <a href="http://127.0.0.1:8888/home/mysql/">phpMyAdmin Login</a> | Today's Date is <i><?php echo date("m/d/y"); ?></i> | Based on <a href="http://helpdesk.hostmonster.com/index.php/kb/browse/000942"><i>Hostmonster Knowledgebase</i></a></center></body></html>

and chat.php

<?phpif(isset($_POST['startChat'])){  	 if($_POST['custName'] != ""){  		 $_SESSION['custName'] = stripslashes(htmlspecialchars($_POST['custName']));  		 $_SESSION['problemCategory'] = stripslashes(htmlspecialchars($_POST['probType']));		 $_SESSION['customerQuestion'] = stripslashes(htmlspecialchars($_POST['usrQuestion']));	 }  	 else{  		 header(userform.php);  	 }  }  ?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Live Chat Support v0.2 - Chat with an Operator</title></head><body><center><h1>Chat v0.1.1 - Live Chat</h1></center><?php$problemCategory = $_POST['probType'];$customerQuestion = $_POST['usrQuestion'];?><center><table border="1" width="500" cellpadding="5"><tr><td><center>Chat Options - <a href="java script: self.close()">Close Window</a> - <a href="log.html">Print Log</a></center></td></tr><tr valign="top"><td>Welcome to Live Chat Support, <b><?php echo $_SESSION['custName']; ?></b>. Your question was: "<?php echo $_SESSION['customerQuestion']; ?>".</td></tr><tr height="400"><td><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>  </td></tr><tr><td><center><form><input type="text" size="60" id="userInput" name="userInput" /><input type="submit" value="Send" id="sendMsg" name="sendMsg" /></form><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script><script type="text/javascript">// If user submits the form  $("#sendMsg").click(function(){  var clientmsg = $("#userInput").val();  $.post("post.php", {text: clientmsg});  $("#userInput").attr("value", "");  return false;  });  //Load the file containing the chat log  function loadLog(){	   $.ajax({  url: "log.html",  cache: false,  success: function(html){  $("#chatbox").html(html); //Insert chat log into the #chatbox div  },  });  }  setInterval (loadLog, 1000);	// Reload file every 2500 ms or x ms if you wish to change the second parameter  </script></center></td></tr></table></center></body></html>

and

<?phpsession_start();  if(isset($_SESSION['custName'])){  $text = $_POST['text'];    $fp = fopen("log.html", 'a');  fwrite($fp, "<div>(".date("g:i A").") <b>".$_SESSION['custName']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");  fclose($fp);  }  ?>

Link to comment
Share on other sites

where do you call the log updating script?Try adding a var_dump($_GET)and var_dump($_SESSION)to see if you have any variablesPS: I would rather use a database than a file to store chat messages

Link to comment
Share on other sites

But what if they are in the same directory?
Well, you tell me. Does it work when you do that? It's not hard to try:
<?phpini_set('display_errors', 1);error_reporting(E_ALL);if(file_exists("log.html")){  echo 'file exists';}else{  echo 'file does not exist';}?>

Link to comment
Share on other sites

I don't have time to read the code, but you may find this interesting: http://tutorialzine.com/2010/10/ajax-web-chat-php-mysql/
That is one approach, but the better way is to have prolonged HTTP request, and have the server push data to the browser instead of the browser requesting it on a time limit. I remember Synook telling me about this practice which is called Comet. I have no idea how you'd go about implementing this though. I haven't found many tutorials on Comet. If I did have to implement a chat system, I'd probably use one of the Facebook-like chat systems already out there.
Link to comment
Share on other sites

This is interesting. I'd also be interested in learning how it's done. That might explain the <iframe> fb uses for their "like" buttons.
well the iframe FB uses isn't a prolonged HTTP request. It's just placing whatever is in that external resource on your page. You can generate the iframe code here for a FB like button. Comet is used for keeping an HTTP request open, instead of closing it once the client receives the response.
Link to comment
Share on other sites

Well, you tell me. Does it work when you do that? It's not hard to try:
<?phpini_set('display_errors', 1);error_reporting(E_ALL);if(file_exists("log.html")){  echo 'file exists';}else{  echo 'file does not exist';}?>

It exists. So that must mean there is a problem writing to the file and/or updating it?
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...