Page 1 of 1

PHP echo calling AJAX function outside of HTML <script> file

Posted: Wed Sep 30, 2009 3:53 pm
by black_hat
I have three files, for the sake of simplifying the problem:
1. HTML, with javascript include statement in the header
2. AJAX controller file which points method calls from (1) to the correct PHP file in (3)
3. PHP file that does string manipulations, graph rendering, etc.

So here's the scenario. I have a simple HTML page that has an input form and submit button. The Submit calls a "render()" JS method which is read by the AJAX controller and calls a PHP page that renders and echos a graph back to the HTML page.

HTML

Code: Select all

 
<html>
    <head><script type="text/javascript" src="ajax_controller.js"></script></head>
    <body>
    <form>
    ...
        <input type='button' class="submitButton" id="submitButton" onClick='render(...)' value='Submit'/>
    </form>
...
 
AJAX

Code: Select all

 
var xmlhttp;
 
function render(...)
{
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="graph.php"; // get the result and display
    url=url+"?...="+X; // the team selection
    url=url+"&...="+Y; // the stat selection
    xmlhttp.onreadystatechange=getRender;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}
function getRender()
{
    if (xmlhttp.readyState==4)
    {
        document.getElementById("...").innerHTML=xmlhttp.responseText;
    }
}
 
PHP

Code: Select all

 
<?php>
....
echo $graph (...)
....
echo "<a href='#' onClick=\"render({$Y}+1, {$X});return false;\">Re-render</a>"; // TODO:  Echo back new link that re-calls using AJAX
</?>
 
As you can see, once the graph is drawn, I want to put a link under it that the user can click to redraw the graph without refreshing the page (AJAX). Problem is, the PHP file does not have the .JS include (and shouldn't, won't) but the echo isn't giving me a link that calls the function correctly. Perhaps I shouldn't use <a href=...>? Is there another way?

Thanks!

I've created a duplicate entry in the Javascript subforum since this may or may not deal with both languages. Once an answer solves my question or points me in the right direction, I shall delete the other. Thanks for understanding.

Re: PHP echo calling AJAX function outside of HTML <script> file

Posted: Wed Sep 30, 2009 3:57 pm
by CodeGeek
I don't know much and I'm probably wrong, but could it be that you haven't defined $graph?

Re: PHP echo calling AJAX function outside of HTML <script> file

Posted: Wed Sep 30, 2009 4:05 pm
by black_hat
I'm sorry I didn't explain that part better - the code you see above is a cut-up version. The AJAX form submit works and renders the graph. Now I want to click a link that was echoed from PHP with the graph that would, when pressed, re-draw the graph through the same AJAX controller.

basically a direct call to an AJAX function rather than through a form Submit.
So... <... onClick("render(X,Y)") ></>

AND

Will this echoed statement work if the actual onClick call isn't written in the HTML where the AJAX controller is included?

Re: PHP echo calling AJAX function outside of HTML <script> file

Posted: Wed Sep 30, 2009 4:11 pm
by CodeGeek
I am sorry then I can't help, all I can suggest is that you review your code and check it through for faults, that's normally what is wrong with my codes.