PDA

View Full Version : ajax mysql php


itgetsharder
05-08-2009, 07:26 PM
Hi, im pretty new to this. basically, i have two database tables in mysql, one called items(id of the book, cookieid, quantity) and the other books(id, name, desc, price). my php returns xml(name, price, quantity and total amount). my code returns the name and price but i can't get it to return the quantity or total.

can anyone see any errors in my code? i think the problem is with the quantity which is the variable newCell2. I keep getting the error "object expected" with that part in IE.


if (request.readyState==4) {
if(request.status==200) {

var response =request.responseXML;
for(var i=0; i<=response.getElementsByTagName('store').length; i++)
{
var newRow = document.getElementById('table').insertRow(-1); // Insert the third row
var newCell1 = newRow.insertCell(0); // Insert the first cell
newCell1.innerHTML = response.getElementsByTagName('book')[i].childNodes[1].childNodes[0].nodeValue; // First cell's innerHTML
var newCell3 = newRow.insertCell(2); // Insert the second cell
newCell3.innerHTML = response.getElementsByTagName('book')[i].childNodes[3].childNodes[0].nodeValue;
var newCell2 = newRow.insertCell(1); // Insert the second cell
newCell2.innerHTML = response.getElementsByTagName('book')[i].childNodes[4].childNodes[0].nodeValue; // Second cell's innerHTML
var newCell4 = newRow.insertCell(3); // Insert the second cell
newCell4.innerHTML = response.getElementsByTagName('book')[i].childNodes[5].childNodes[0].nodeValue;
}

thanks in advance.

bdl
05-08-2009, 08:53 PM
Would you please post a valid XML example (that this script expects)? Please use the CODE tags when posting.

Also, consider using JSON for this, as its lightweight and much easier to handle than XML.

itgetsharder
05-08-2009, 08:59 PM
thanks for the reply. the xml is embedded in the php, but i know that the php and sql db are correct.

echo '<?xml version="1.0" encoding="ISO-8859-1"?><store>';
while($row = mysql_fetch_array($result1))
{

echo "<book>";
echo "<id>".$row['id']."</id>";
echo "<name>".$row['name']."</name>";
echo "<description>".$row['description']."</description>";
echo "<quantity>".$count."</quantity>";
echo "<price>".$row['price']."</price>";
echo "<sum>".$count*$row['price']."</sum>";
echo "</book>";

}
}

}

}
echo "</store>";

any ideas?

bdl
05-09-2009, 02:22 AM
No, I was looking for the actual XML. Pull up that PHP script in your browser and post the XML with content. You need to start troubleshooting with the actual data the Ajax request sees.

rnd me
05-09-2009, 09:38 PM
in general, it's more reliable to use getElementsByTagName('XXX') than childNodes, which can change between files and browsers.

itgetsharder
05-10-2009, 04:59 PM
thank you for your reply.
i can get the name decription and price to show but the quantity and sum won't show. ill keep trying.

A1ien51
05-11-2009, 04:42 AM
You probably have a whitespace node issue.

Eric