View Full Version : Javascript calling a PHP function on a same page

01-01-2012, 11:48 PM
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:
<html ><head><title>Untitled 1</title></head>

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(){

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

$xmlDoc = new DOMDocument();

$proc = new XSLTProcessor();
echo $proc->transformToXML($xmlDoc);

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

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


<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl">
<xsl:template match="data">
<xsl:apply-templates select="./weather" />

<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>

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


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

Old Pedant
01-02-2012, 02:35 AM
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.

01-02-2012, 10:57 AM
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.

Old Pedant
01-02-2012, 09:29 PM
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.