T
The Mysterious Mr. Rocco
Hi,
I have an xml file which has user defined data, and I'm trying to use
php to write the data
into an array of Javascript structs. The code I have as follows is:
<html>
<head>
</head>
<body>
<script type="text/javascript">
var foundresults=new Array();
<?php
$doc = new DOMDocument();
$doc->load( 'results.xml' );
$counter=0;
$results = $doc->getElementsByTagName( "company" );
foreach( $results as $company )
{
$authors = $company->getElementsByTagName( "name" );
$name = $authors->item(0)->nodeValue;
$publishers = $company->getElementsByTagName( "postcode" );
$postcode = $publishers->item(0)->nodeValue;
$titles = $company->getElementsByTagName( "url" );
$url = $titles->item(0)->nodeValue;
echo "foundresults[$counter] = {company: '$name', postcode:
'$postcode', url: '$url'}; ";
$counter++;
}
?>
</script>
<script type="text/javascript">
for(i=0;i<foundresults.length;i++)
{
document.write(foundresults.name + ' ' + foundresults.postcode +
' ' + foundresults.url +'<br>');
}
</script>
</body>
</html>
The document.write(...) statement is simply to test that the
"foundresults" array is being populated and accessible outside the
first javascript brace. However, nothing is being outputted to screen.
But when I view source of the document, I get:
<html>
<head>
</head>
<body>
<script type="text/javascript">
var foundresults=new Array();
foundresults[0] = {company: '
Webaholics
', postcode: '
KT12 3NY
', url: '
http://www.webaholics.com
'}; foundresults[1] = {company: '
Webfiends
', postcode: '
KT12 3PP
', url: '
http://www.webfiends.com
'};
</script>
<script type="text/javascript">
for(i=0;i<foundresults.length;i++)
{
document.write(foundresults.name + ' ' + foundresults.postcode +
' ' + foundresults.url +'<br>');
}
</script>
</body>
</html>
Despite the strange line breaks in the population of the foundresults
array in the javascript, the code looks fine to me, and I am
mystified as to why the code isn't working. The php part is definitely
reading the data in correcttly (I tested that thoroughly!) but the
javascript is in a sulk.
Can anyone please help?
Best wishes
Trev
I have an xml file which has user defined data, and I'm trying to use
php to write the data
into an array of Javascript structs. The code I have as follows is:
<html>
<head>
</head>
<body>
<script type="text/javascript">
var foundresults=new Array();
<?php
$doc = new DOMDocument();
$doc->load( 'results.xml' );
$counter=0;
$results = $doc->getElementsByTagName( "company" );
foreach( $results as $company )
{
$authors = $company->getElementsByTagName( "name" );
$name = $authors->item(0)->nodeValue;
$publishers = $company->getElementsByTagName( "postcode" );
$postcode = $publishers->item(0)->nodeValue;
$titles = $company->getElementsByTagName( "url" );
$url = $titles->item(0)->nodeValue;
echo "foundresults[$counter] = {company: '$name', postcode:
'$postcode', url: '$url'}; ";
$counter++;
}
?>
</script>
<script type="text/javascript">
for(i=0;i<foundresults.length;i++)
{
document.write(foundresults.name + ' ' + foundresults.postcode +
' ' + foundresults.url +'<br>');
}
</script>
</body>
</html>
The document.write(...) statement is simply to test that the
"foundresults" array is being populated and accessible outside the
first javascript brace. However, nothing is being outputted to screen.
But when I view source of the document, I get:
<html>
<head>
</head>
<body>
<script type="text/javascript">
var foundresults=new Array();
foundresults[0] = {company: '
Webaholics
', postcode: '
KT12 3NY
', url: '
http://www.webaholics.com
'}; foundresults[1] = {company: '
Webfiends
', postcode: '
KT12 3PP
', url: '
http://www.webfiends.com
'};
</script>
<script type="text/javascript">
for(i=0;i<foundresults.length;i++)
{
document.write(foundresults.name + ' ' + foundresults.postcode +
' ' + foundresults.url +'<br>');
}
</script>
</body>
</html>
Despite the strange line breaks in the population of the foundresults
array in the javascript, the code looks fine to me, and I am
mystified as to why the code isn't working. The php part is definitely
reading the data in correcttly (I tested that thoroughly!) but the
javascript is in a sulk.
Can anyone please help?
Best wishes
Trev