JonesJ
01-01-2012, 10:48 PM
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" xmlns:php="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 :p on the second line of the last code is ':' and 'p'.
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" xmlns:php="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 :p on the second line of the last code is ':' and 'p'.