Jump to content

[SOLVED] Refreshing & Sending


spamoom

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/84295-solved-refreshing-sending/
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.