Jump to content

[SOLVED] Cannout output echo value with AJAX


Anzeo

Recommended Posts

Hi all,

 

I have a problem which is driving me mad. I just started on AJAX and I was able to make a simple function that shows you some text when you click on an image.

 

However, I wanted to extend my PHP-framework (based on TemplatePower) with AJAX and so I started to work on a new implementation. However, I do not receive any output anymore. I looked over my code again and again, but I can't pinpoint where I'm wrong.

 

index.php (this makes the template object and outputs it to the browser window.

include("header.php");// contains template power and the constant definitions
    
    // Setup template
    $tpl = new TemplatePower(TEMPLATE_PATH."main.tpl");
    $tpl->prepare();

    $tpl->assign("TITLE", "De Poef Online");

    $tpl->printToScreen();

 

This is the current HTML file i'm using (it's actually a .tpl filed but it's being parsed to html thanks to TemplatePower)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta http-equiv="refresh" content="{REDIRECT_DELAY};url={REDIRECT_URL}" />
        <link rel="stylesheet" href="dpo.css" />
        <title>{TITLE}</title>
        <script type="text/javaScript" src="./scripts/CalendarPopup.js"></script>
        <script type="text/javaScript" src="./scripts/dispatch.js"></script>
    </head>
    <body>
        <div id="header"><h1>De Poef Online</h1></div>
        <div id="body">
            <a href="" onclick="dispatch('test')">test</a>
            <div id="test">. ....</div>
        </div>
    </body>
</html>

This is the javascript

var xmlhttp;

function dispatch(action)
{
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
        alert ("Your browser does not support HTTP Requests");
        return;
    }

    var url="dispatcher.php";
    url=url+"?action="+action;
    url=url+"&sid="+Math.random();      // add a random part to make sure the server won't use a cached file
    
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}

/**
*  This function is called when the state of the httprequest has changed
*/
function stateChanged()
{
    if (xmlhttp.readyState==4) {
        document.getElementById('test').innerHTML="Done";
    } else {
        document.getElementById('test').innerHTML="Loading";
    }
}

/**
*  Autodetects which XML-HTTP Object should be fetched
*/
function GetXmlHttpObject()
{
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject)
    {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}

dispatch.php contains a simple echo statement

 

EDIT: He's flashing me the right text but then he deletes it again :/

 

EDIT2: Ok my responseText seems to be empty, it's not returning anything.

also if I ad an alert in front of the innerhtml lines the output stays on the screen, otherwise it reloads -___-0

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.