Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    40
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Javascript calling a PHP function on a same page

    Hi,
    The point is to have a PHP code on the same page as a javascript calling the PHP function. I am able to make this work when the PHP code is on an external file. But when all but XSL are on the same page, it won't do what it should.

    How to make this work?
    The PHP is correct as it is by its self. And so is the javascript as it is for calling other PHP code. The combination of these two in this case just won't work.









    PHP and Javascript:
    [CODE]
    <html ><head><title>Untitled 1</title></head>

    <body>
    <?php
    function dateToDayOfWeek($xmlDate, $useFormat = 'l')
    {
    $rd = 'Unknown';
    if (count($xmlDate) == 1)
    {
    $element = $xmlDate[0];
    if ($element instanceof DOMElement)
    {
    $rd = date($useFormat, strtotime($element->nodeValue));
    }
    }
    return $rd;
    }

    function axasal(){
    date_default_timezone_set('UTC');

    $xslDoc = new DOMDocument();
    $xslDoc->load("xslfile.xsl"); // Here comes the xsl file name.

    $xmlDoc = new DOMDocument();
    $xmlDoc->load("http://free.worldweatheronline.com/feed/weather.ashx?q=milan,italy&format=xml&num_of_days=5&key=af8b2fc417222733111712");

    $proc = new XSLTProcessor();
    $proc->registerPHPFunctions('dateToDayOfWeek');
    $proc->importStylesheet($xslDoc);
    echo $proc->transformToXML($xmlDoc);
    }
    ?>

    <script type="text/javascript">
    function call_php(){
    document.getElementById("place").innerHTML="<?php echo axasal(); ?>";
    }
    </script>

    <form name = "form">
    <input type = "button" name = "aaa" id = "aaa" value = "Click me" onclick = "call_php()" />
    </form>
    <div id="place"></div>

    </body></html>
    [CODE]



    XSL:
    [CODE]
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlnshp="http://php.net/xsl">
    <xsl:template match="data">
    <html>
    <head><title></title></head>
    <body>
    <table>
    <tr>
    <xsl:apply-templates select="./weather" />
    </tr>
    </table>
    </body>
    </html>
    </xsl:template>

    <xsl:template match="weather">
    <td style="width: 97px">
    <p><xsl:value-of select="php:function('dateToDayOfWeek', date)" /></p>
    <p><xsl:apply-templates select="./weatherIconUrl" /></p>
    </td>
    </xsl:template>

    <xsl:template match="weatherIconUrl">
    <img>
    <xsl:attribute name="src">
    <xsl:value-of select="." />
    </xsl:attribute>
    </img>
    </xsl:template>

    </xsl:stylesheet>
    [CODE]



    The smiley face on the second line of the last code is ':' and 'p'.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,494 Times in 4,458 Posts
    Can't be done.

    You need to understand how client/server code works in a browser/web server system.

    The web server, *INCLUDING* the PHP code, is executed and creates HTML (optionally with JavaScript in the HTML). When the page is fully created, it sends the HTML to the browser.

    At that point, the browser and the web server *COMPLETELY AND UTTERLY DISCONNECT* from each other and neither has any knowledge of the other. The browser doesn't remember the connection to the server. The server doesn't even remember that it *ever* sent HTML to that particular browser.

    In order for the HTML (including JavaScript) to get the web server to do anything more, the browser must make a *BRAND NEW AND COMPLETELY SEPARATE* HTTP request to the server. At which point the server will respond with an HTTP response.

    This brand new HTTP request can come in two basic forms from a web page:
    (1) The web page can submit a <form> or use an <a href="..."> link and the web server will see that <form> or link as an HTTP request; the web page can extract information from the querystring or from POSTed data in the <form>.
    (2) JavaScript in the web page can make an explicit HTTP request using the XMLHttpRequest object built into the browser (or Microsoft.XMLHTTP ActiveX object in older MSIE browsers). The web server actually can't distinguish this request from a <form> or <a> request, so it responds with an HTTP Response that JavaScript can then read and act on.

    But there is no way for JavaScript to simply call a PHP function with no separate HTTP Request/Response protocol.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • The Following 2 Users Say Thank You to Old Pedant For This Useful Post:

    JonesJ (01-02-2012), q1h (01-03-2012)

  • #3
    New Coder
    Join Date
    Jul 2011
    Posts
    40
    Thanks
    16
    Thanked 0 Times in 0 Posts
    In computer science - everything is possible. There may be a way to make this function with the form or page reload. I just haven't found a working solution yet.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,494 Times in 4,458 Posts
    I would suggest AJAX. It can give the *appearance* of calling a PHP function from JavaScript.

    But the thing to remember is that the PHP that services the AJAX call will *NOT* be aware of any variables, etc., that were created/initialized/manipulated in the PHP code that created the page in the first place. To some degree, you can mitigate this by passing along values by using Session "variables", but that's not something you want to overuse.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •