Jump to content

Parse Error


shadowayex

Recommended Posts

I'm trying to make a chatroom type deal using PHP and AJAX. Problem is, when I go to load the page, it tells me this:Parse error: parse error, unexpected T_STRING in /home/www/testtools.freehostia.com/chatroom.php on line 16Line 16 says this:$result = mysql_query("SELECT * FROM chat");I looked through the rest of the PHP and it all looks fine. Here's the code for the pages involved. I've never used AJAX before, so there's probably something wrong with that as well.chatroom.php

<?php session_start();$link = mysql_connect('host', 'username', 'password')    or die('Could not connect: ' . mysql_error());mysql_select_db('database') or die('Could not select database');$user=$_SESSION['user'];$message=mysql_real_escape_string($_POST['message']);if($message!=""){mysql_query("INSERT INTO chat (Username, Message) VALUES ('$user', '$message');}$result = mysql_query("SELECT * FROM chat");while($row = mysql_fetch_array($result)) { echo "$row['Username'] . ": " . "$row['Message']"; echo "<br />"; }mysql_close($link);?><html><head><title>Test Chat</title><script type="text/javascript" src="chat.js"></script></head><body><div id="txtHint"></div><form action="chatroom.php" method="post"><input type="text" name="message" value="" /><input type="submit" name="submit" value="Send" /></form></body></html>

chat.js

var xmlHttpfunction showUser(str){ xmlHttp=GetXmlHttpObject()if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return }var url="chatroom.php"xmlHttp.onreadystatechange=stateChanged xmlHttp.open("POST",url,true)xmlHttp.send(null)}function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {  document.getElementById("txtHint").innerHTML=xmlHttp.responseText  } }function GetXmlHttpObject(){var xmlHttp=null;try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); }catch (e) { //Internet Explorer try  {  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");  } catch (e)  {  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");  } }return xmlHttp;}

This is the first time I've ever used Ajax and the next step after I get the messages to show is to figure out how to make it actually move on it's own. I assume that if this actually worked, the user would have to refresh to see new messages.

Link to comment
Share on other sites

Change this:if($message!=""){mysql_query("INSERT INTO chat (Username, Message) VALUES ('$user', '$message');}for this:if($message!=""){mysql_query("INSERT INTO chat (Username, Message) VALUES ('$user', '$message')");}

Link to comment
Share on other sites

Good call. I feel slightly dumb for not catching that. Now it says there's another error:Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/www/testtools.freehostia.com/chatroom.php on line 20Line 20 Reads: echo "$row['Username']" . ": " . "$row['Message']";

Link to comment
Share on other sites

You have to delimit the variables with curly braces

echo "{$row['Username']} : {$row['Message']}";

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...