javascript - AJAX XMLHttpRequest 'bouncing back' when sending vars to PHP

274

I apologize if I don't articulate my problem correctly, but I'll give it my best shot. I've been looking all over the net for info which can help me with this issue, to no avail. Just a bit of background. I'm an experienced web coder, though haven't done webwork in a few years prior to this, I have done a fair bit of work in PHP and javascript before and these days I work with C++, so I'm fairly experienced with programming principles.

I'm building some blog software, and inb4wordpress and jQuery, I simply don't care. So spare it please... I'm loading some blog entries into an element through a simple AJAX request function. This function is detailed below: ( It's been changed to a 3 function example I found on the net while I was trying to debug this issue, no one's 'simple' code seems to work. )

The problem is detailed beneath the code.

var httpObject = null;

function getHTTPObject(){
    if(window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
    else if(window.XMLHttpRequest) return new XMLHttpRequest();
    else {
        alert("Your browser does not support AJAX.");
        return null;
    }
}

function setOutput(){
    if(httpObject.readyState == 4){
        document.getElementById("entries").innerHTML = httpObject.responseText;
    }
}

function loadEntries(s) {
    httpObject = getHTTPObject();
    if (httpObject != null) {
        httpObject.open("GET","entries.php?" + s,true);
        httpObject.send(null);
        httpObject.onreadystatechange = setOutput;
    }
}

Simple stuff? I can't seem to see any errors there. This is how the function is called:

        <div id='entries'>
        <script type="text/javascript">
            loadEntries('blog=<?php echo $process['id']; ?>&page=0');
        </script>
    </div>

also simple. Here's the PHP code for 'entries.php':

<?php
    require_once('inc/bloginc.php');

    if(isset($_GET['page'])) {
        $page = intval($_GET['page']);
    } else $page = 0;

    $entries = 3;
    $init = $page * $entries;
    $limit = $entries + $init;

    if(!isset($_GET['blog'])) die("WTF DIE");
    else $blog = mysql_real_escape_string($_GET['blog']);

    $tag = '';
    if(isset($_GET['tag'])) {
        $tag = mysql_real_escape_string($_GET['tag']);
        echo "<span class='blogEntryBody'>viewing entries tagged with: '" . $tag . "' / <a href='' onclick=\"";
        echo "loadEntries('blog=" . $blog . "')";
        echo "\">clear?</a></span></br>";
        echo "<hr>";
    }

    $numposts = nResults($blog, $tag);
    buildEntries(getEntries($blog, $tag, $init, $limit));

    if($numposts > $entries) {
        echo "</br><span class='blogEntryBody'>";
        if($page > 0) {
            echo "<a href='' onClick=\"";
            echo "loadEntries('blog=" . $blog;
            if(isset($_GET['tag'])) echo "&tag=" . $tag;
            echo "&page=" . (--$page) . "')";
            echo "\">Previous Entries</a>";
            echo " / ";
        }

        echo "<a href='' onClick=\"";
        echo "loadEntries('blog=" . $blog;
        if(isset($_GET['tag'])) echo "&tag=" . $tag;
        echo "&page=" . (++$page) . "')";
        echo "\">Next Entries</a>";

        echo "<br></span>";
    }

?>

okay, now here's where things get tricky:

When sending vars to 'entries.php', such as: entries.php?blog=walk&page=1

They intermittently work, some of these work, but some don't.

I know it's not the PHP code, since loading entries.php up in a new window and manually passing these vars elicits the desired results. What happens is that the HTTP GET request returns 'undefined' in Firefox webdev console, such as this:

[14:47:33.505] GET http://localhost/meg/entries.php?blog=walk&tag=lorem [undefined 2ms]

^ The 'tag' variable usually works, it's normally the 'page' variable that sends everything haywire.

What happens is, after clicking 'next page', you quickly see a blank div, and then it quickly bounces back to the previous state. You see all this loading in the console. It'll return 'undefined' then reload the previous state. Which is just puzzling.

I don't understand why this would be occurring.

I hope I've provided enough information, and set it out in an easy to understand format. I'm new to asking questions. I usually just 'googleit' or RTM. But I think maybe this time someone else will have seen this before.

Oh, and I've tested in chrome, same issue. I'm really puzzled, but open to the possibility that maybe I've overlooked something small and crucial.

Thanks!

169

Answer

Solution:

Well it happens few times that ajax doesn't works in chrome & explorer so my suggestion to use jquery because in jquery they already include codes for explorer and chrome.

you can use$.get , $.post or $.ajax methods easily.

People are also looking for solutions to the problem: php - how to pass the view id to the CJuiDialog box?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.