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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.