Saturday, August 25, 2012

PHP load as XML in JQuery

I am trying to load in XML to populate a search box. The code I have works fine with a static xml file, but I need to load in a PHP file to generate a dynamic XML file based on data from a database.


I knwo the PHP file generates the XML as it loads in a browser and the page source shows the correct nodes etc, however when I reference the .php file in my JavaScript it fails to load, or show an error… Using the .xml file (which is a replica of the php output source) it loads fine.


I would like someone to check to see if I am encoding the XML correctly or missing something in my JavaScript… and advice will be greatly appreciated.



JS

var myArr = [];
$.ajax({
url: \"people.php\", // change to full path of file on server
type: \"GET\",
dataType: \"xml\",
success: parseXml,
complete: setupAC,
failure: function(data) {
alert(\"XML File could not be found\");
}
});

function parseXml(xml){
$(xml).find(\"person\").each(function(){
var thisItem = $(this).find(\'name\').text();
myArr.push(thisItem);
alert(thisItem);
});
}

PHP

<?php

include \'../../inc_global/connect.php\';

$query = \'SELECT * FROM candidates\';

$result = mysql_query($query, $link);

$xmlOutput = \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\\n\";
$xmlOutput .= \"<people>\\n\";

while ($line = mysql_fetch_assoc($result)) {

$xmlOutput .= \"<person>\\n\";
$xmlOutput .= \"<name>\" . $line[\'name\'] . \"</name>\\n\";
$xmlOutput .= \"</person>\\n\";

}

$xmlOutput .= \"</people>\\n\";

echo $xmlOutput;

mysql_close($link);

?>






Rating: 3 out of 5 based on 3 ratings



The post PHP load as XML in JQuery appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/php-load-as-xml-in-jquery.html

No comments:

Post a Comment