I have a select menu being populated by an Ajax call to a php script that returns the list of products from a table in XML form.
The product table has about 400 records and for some reason, the select menu displays nothing with a select statement that returns all. Troubleshooting with Firebug, I can see the php code is successfully returning ALL 400 records correctly to the browser but it does not get displayed.
When I alter the select statement to be “SELECT ProductID,ProductName FROM product WHERE ProductID < 74”, it works, displaying the first 73 items. Thinking this could possibly be due to something wrong with item 74, I altered the where clause to “> 74″ but this also returned nothing. A where clause of ” > 74 and < 120″ however works.
What could possibly be the cause? Please find the code snippets below.
The javascript of the code is as follows:
function populateProducts(xmlindata) {
var mySelect = $('#ili_product');
$(xmlindata).find("Product").each(function()
{
optionValue=$(this).find("id").text();
optionText = $(this).find("name").text();
mySelect.append($('<option></option>').val(optionValue).html(optionText));
});
}
The php code is:
<?php
include("dbconfig.inc.php");
header("Content-type: text/xml");
echo "<?xml version=\"1.0\" ?>\n";
echo "<products>\n";
$select = "SELECT ProductID,ProductName FROM product";
try {
foreach($dbh->query($select) as $row) {
echo "<Product>\n\t<id>".$row['ProductID']."</id>\n\t<name>".$row['ProductName']."</name>\n</Product>\n";
}
}
catch(PDOException $e) {
echo $e->getMessage();
die();
}
echo "</products>";
?>
It would be a good practice to escape quotes and other characters while generating/forming html based on xml/json data
In your case,
optionValue=$(this).find("id").text().replace('\'','"');or
replace("'", "’");This way you escape quotes, if they are the ones causing the problem. Not sure if the problem is due to breaking of the strings.
you can escape multiple characters like this.
.replace(/&/g, "&").replace(/'/g, """);Notice
//Regex and thegflag, this will find the characters globally.