Beffic Posted November 19, 2008 Share Posted November 19, 2008 How can I change the request in here from the chat.txt file to a mysql request? function ajax_read(url) { if(window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); if(xmlhttp.overrideMimeType){ xmlhttp.overrideMimeType('text/xml'); } } else if(window.ActiveXObject){ try{ xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){ } } } if(!xmlhttp) { alert('Giving up Cannot create an XMLHTTP instance'); return false; } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState==4) { document.getElementById("chatwindow").innerHTML = xmlhttp.responseText; zeit = new Date(); ms = (zeit.getHours() * 24 * 60 * 1000) + (zeit.getMinutes() * 60 * 1000) + (zeit.getSeconds() * 1000) + zeit.getMilliseconds(); intUpdate = setTimeout("ajax_read('chat.txt?x=" + ms + "')", waittime) } } xmlhttp.open('GET',url,true); xmlhttp.send(null); } Quote Link to comment Share on other sites More sharing options...
Beffic Posted November 20, 2008 Author Share Posted November 20, 2008 Okay, I'll try to be a little more clear. How can I change the request used in this frame from a txt request so that instead I can retrieve the chat information from a mysql database instead? Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 20, 2008 Share Posted November 20, 2008 You can't do this witm JavaScript alone. You need to send a request to server side script, that will then get data from database and send it back to your JavaScript. Quote Link to comment Share on other sites More sharing options...
Beffic Posted November 20, 2008 Author Share Posted November 20, 2008 How would that look? Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 20, 2008 Share Posted November 20, 2008 How exactly it is working now? Quote Link to comment Share on other sites More sharing options...
Beffic Posted November 21, 2008 Author Share Posted November 21, 2008 Well changing "intUpdate = setTimeout("ajax_read('chat.txt?x=" + ms + "')", waittime)" to "intUpdate = setTimeout("ajax_read(mysql_query("SELECT *FROM Chat"))", waittime)" doesn't seem to work. Quote Link to comment Share on other sites More sharing options...
corbin Posted November 21, 2008 Share Posted November 21, 2008 Don't take this the wrong way, but you obviously either have no idea how PHP/AJAX work, or you have no idea how they work together. AJAX simply emulates a request to a server and allows you to manipulate the content. PHP is run 100% (in this context) server side; therefore, it would not make sense to try to put PHP code in JavaScript. You will need the AJAX script to send the request to a PHP page which will handle the request and return the desired results. Quote Link to comment Share on other sites More sharing options...
Beffic Posted November 21, 2008 Author Share Posted November 21, 2008 Yeah, I am completely new to ajax and it probably makes no sence but I learn more when I can take a code and edit it myself. But I need a place to start from... It's how I learned php. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted November 21, 2008 Share Posted November 21, 2008 Start small, follow their example(s). Read: http://www.w3schools.com/Ajax/ajax_intro.asp Learn By Example: http://www.w3schools.com/PHP/php_ajax_database.asp Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 21, 2008 Share Posted November 21, 2008 In your case, you need chat.txt file to be dynamically filled with data from database... Well.. you might as well rename it to chat.php Quote Link to comment Share on other sites More sharing options...
Beffic Posted November 22, 2008 Author Share Posted November 22, 2008 Okay, so I think I understand a little better but would I have to change the ajax_read to a ajaxrequest.open since it's not reading a text file anymore but from a database? Quote Link to comment Share on other sites More sharing options...
corbin Posted November 22, 2008 Share Posted November 22, 2008 Your ajax_read function should still work. You are essentially still reading from a file. JS doesn't know the difference between a plain text file and a PHP file (for these purposes anyway). All it knows is that it got back the text that was sent to it. Therefore, a text file and a PHP file can be requested the same way. Quote Link to comment Share on other sites More sharing options...
Beffic Posted November 22, 2008 Author Share Posted November 22, 2008 Wow!!! Thanks!!!! I got it now and I understand ajax 50% more! Thanks again guys! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.