spamoom Posted January 3, 2008 Share Posted January 3, 2008 I know this has already been asked multiple times and I am sorry to post it again, but I feel that I can learn more if it is code that I've been working with. Well, here it goes. I have been making a little shout box script for my website, using Ajax to show the last 5 messages and to post messages via a text box all without loading the page. Seems simple enough. In my main page I have the following, <div id="shoutBox"><a href="javascript: viewShout(<?php echo $corpID; ?>)">Click To Start Chat</a></div> <input type="text" id="chatText" /> <input type="submit" Value="Send" onclick="postShout(document.getElementById('chatText').value);" /> When I click on 'Click to start chat' It does as I would have wanted to and displays the last 5 results, but no refreshing happening. Here is the javascript function createRequestObject() { var req; if(window.XMLHttpRequest){ // Firefox, Safari, Opera... req = new XMLHttpRequest(); } else if(window.ActiveXObject) { // Internet Explorer 5+ req = new ActiveXObject("Microsoft.XMLHTTP"); } else { // There is an error creating the object, // just as an old browser is being used. alert('There was a problem creating the XMLHttpRequest object'); } return req; } // Make the XMLHttpRequest object var http = createRequestObject(); function sendShoutRequest(act) { // Open PHP script for requests http.open('get', 'ajax.php?shoutCheck='+act); http.onreadystatechange = handleShoutResponse; http.send(null); } function handleShoutResponse(act) { if(http.readyState == 4 && http.status == 200){ // Text returned FROM PHP script var response = http.responseText; if(response) { // UPDATE ajaxTest content document.getElementById("shoutBox").innerHTML = response; setTimeout(viewShout,2000); } } } function viewShout(act) { sendShoutRequest(act); } Some of you may notice that I learnt this from the tutorial on this site =D. Anyway, as I said it doesn't refresh, now even stranger. I woke up this morning and clicking on the link shows the results, then disappears after my time out time. Ie if I set my timeout to 1000, my content disappears after 1 second. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 4, 2008 Share Posted January 4, 2008 look at this thread that is similar to yours; that I have already posted the answer to. you should be able to see how to do this from there - good luck http://www.phpfreaks.com/forums/index.php/topic,171649.msg760084.html#msg760084 Quote Link to comment Share on other sites More sharing options...
spamoom Posted January 4, 2008 Author Share Posted January 4, 2008 EDIT: I can verify that the script refreshes for some reason. The problem I had was passing the var I needed on. I had to set a global var on my page in order to pass it the script. 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.