dcole.ath.cx Posted August 17, 2007 Share Posted August 17, 2007 I'm working on a script that you enter something, it is then found in a data bank and the next column over is retrieved. I have removed all PHP and MySQL errors, yet the script doesn't work. Here is the code for each file (3 total)chat.html <html><head><title>AJAX Hello World Test Page</title><link rel="stylesheet" href="http://www.hackorama.com/css/common.css" type="text/css" title="default" media="screen"><script type="text/javascript" src="ajax.js"></script></head><body><p><input id="testmsg" type="text" value=""><button onclick="talktoServer()">Say Hello to Server</button><div id="msg_display" style="{ background: yellow; font-weight: bold; }">The data from the server will go here...</div></body></html> ajax.js function talktoServer(){ var req = newXMLHttpRequest(); //register the callback handler function var callbackHandler = getReadyStateHandler(req, updateMsgOnBrowser); req.onreadystatechange = callbackHandler; req.open("POST", "server.php", true); req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //get the value from the text input element and send it to server var testmsg = document.getElementById("testmsg"); var msg_value = testmsg.value; req.send("msg="+msg_value);}// This is the callback functions that gets called// for the response from the server with the XML datavar lastPing = 0;function updateMsgOnBrowser(testXML) { var test = testXML.getElementsByTagName("test")[0]; var message = testXML.getElementsByTagName("message")[0]; var ip = testXML.getElementsByTagName("ip")[0]; var timestamp = test.getAttribute("timestamp"); if (timestamp > lastPing) { lastPing = timestamp; var ip_value = ip.firstChild.nodeValue; var message_value = message.firstChild.nodeValue; var msg_display = document.getElementById("msg_display"); msg_display.innerHTML = " Server got the message: \"" + message_value + "\"" + "<br>Server IP: "+ ip_value + " Server Timestamp: \""+ timestamp + "\""; }}//the following two functions are helper infrastructure to //craete a XMLHTTPRequest and register a listner callback functionfunction newXMLHttpRequest() { var xmlreq = false; if (window.XMLHttpRequest) { xmlreq = new XMLHttpRequest(); } else if (window.ActiveXObject) { // Try ActiveX try { xmlreq = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e1) { // first method failed try { xmlreq = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { // both methods failed } } } return xmlreq;} function getReadyStateHandler(req, responseXmlHandler) { return function () { if (req.readyState == 4) { if (req.status == 200) { responseXmlHandler(req.responseXML); } else { var hellomsg = document.getElementById("hellomsg"); hellomsg.innerHTML = "ERROR: "+ req.status; } } }} server.php <?// Connect to MySQL using this data:$dbhost = 'localhost';$dbuser = 'root';$dbpass = '';$dbname = 'chat';// "talk" is name of MySQL table// Connect to MySQL and a table within it$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');mysql_select_db($dbname);// Get message and timeheader('Content-Type: application/xml');$msg = htmlentities(trim(stripslashes($_REQUEST['msg'])));$msg = 'Hi';//$msg could be 'Hello' for example$ts = time();$ip = gethostbyname("ledgerpad.ath.cx");$answer = '';//front is first 6 letters$front = substr($msg, 6);if($front == 'learn:'){ // insert data into MySQL $back = strstr($msg, 'learn:'); list($message, $response) = explode("<><><>", $back); $query = "INSERT INTO talk (message, response) VALUES ($message, $response)"; mysql_query($query) or die('Error, insert query failed'); $answer = 'Thank you, data has been learned.';}else { // get data from MySQL $query = 'SELECT message, response FROM talk'; $result = mysql_query($query); while(list($message,$response)= mysql_fetch_row($result)) { if ($message == $msg) { $answer = $response;$answer = 'Can not'; } else { $answer = 'I am sorry my responses are limited.'; } }}// Close MySQLmysql_close($conn);$msg = $answer;print ("<" . "?xml version=\"1.0\"?" . ">");print ("<test timestamp=\"$ts\">");print ("<ip>$ip</ip>");print ("<message>$msg</message>");print ("</test>");?> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 17, 2007 Share Posted August 17, 2007 If I comment out the MySQL code and make server.php this: <?php$ts = time();$ip = gethostbyname("ledgerpad.ath.cx");$msg = 'Test message';print ("<" . "?xml version=\"1.0\"?" . ">");print ("<test timestamp=\"$ts\">");print ("<ip>$ip</ip>");print ("<message>$msg</message>");print ("</test>");?> It works fine for me in Opera. One thing I noticed from the AJAX is that you don't support the IE AJAX objects, but that's easy enough to change later on. But the AJAX works fine in Opera, it displays the correct data that was returned by the PHP script. So the problem has to be something with the database code. What are you seeing happen that's not right? What happens if you just load the PHP script in a browser? Link to comment Share on other sites More sharing options...
dcole.ath.cx Posted August 18, 2007 Author Share Posted August 18, 2007 I get an XML error because the php script tries to return XML. I'll change the script up a little bit so I can input a message through the URL and see what happens.the files are located at http://ledgerpad.ath.cx/chat/ if you want to see any in action. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 20, 2007 Share Posted August 20, 2007 You could just comment out the header line to check for errors, or something I do fairly often is just have it write out a log file with file_put_contents as it executes and then you can check the log. That won't show PHP errors, but you can write out status messages and see what's happening. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.