I have a problem in building a shopping cart from a catalogue Whenever I enter a new item, the previous item is replaced by the new one.
In the code given below I have an xml file, goods.xml. In showxml.php , I show items with quantityAvailable>0. then through getDoc1 , I update the xmlfile and I also want to update the shopping cart. The problem is that when I select a new item, the previous item is replaced by new one. Please help me. Thanks in advance
My code is given below
XML file: goods.xml
<?xml version="1.0" encoding="utf-8"?>
<items>
<item>
<itemno>1</itemno>
<sellerid>1</sellerid>
<itemname>jj</itemname>
<unitprice>90</unitprice>
<QuantityAvailable>-9</QuantityAvailable>
<ItemDescription>jnjhjh</ItemDescription>
<QuantitySold>0</QuantitySold>
<QuantityHold>17</QuantityHold>
</item>
<item>
<itemno>2</itemno>
<sellerid>1</sellerid>
<itemname>jj</itemname>
<unitprice>90</unitprice>
<QuantityAvailable>2</QuantityAvailable>
<ItemDescription>jnjhjh</ItemDescription>
<QuantitySold>0</QuantitySold>
<QuantityHold>20</QuantityHold>
</item>
</items>
JavaScript function:
unction getDoc1(itemno)
{
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4) /* && xmlhttp.status==200) */
{
document.getElementById("updatexml").innerHTML=xmlhttp.responseText;
}
}
var url="updatexml.php";
url=url+"?itemnum="+itemno;
xmlhttp.open("GET",url,true);
xmlhttp.send(null); }
PHP: showxml.php:
<?php
$xmlFile = "../../data/Assign/goods.xml";
$doc= DOMDocument::load($xmlFile);
$item = $doc->getElementsByTagName("item");
echo"Shopping Catalogue";
echo "<table border=1><tr><th>ItemNo</th><th>ItemName</th><th>Description</th><th>Price</th><th>Quantity Available</th><th>Add To Cart</th></tr>";
foreach($item as $node)
{
$itemno = $node->getElementsByTagName("itemno");
$itemno = $itemno->item(0)->nodeValue;
$itemname = $node->getElementsByTagName("itemname");
$itemname = $itemname->item(0)->nodeValue;
$itemdes = $node->getElementsByTagName("ItemDescription");
$itemdes= $itemdes->item(0)->nodeValue;
$price = $node->getElementsByTagName("unitprice");
$price = $price->item(0)->nodeValue;
$quant = $node->getElementsByTagName("QuantityAvailable");
$quant = $quant->item(0)->nodeValue;
if($quant>0)
echo" <tr><td>{$itemno}</td><td>{$itemname}</td><td>{$itemdes}</td><td>{$price}</td><td>{$quant}</td><td><form> <input type='button' name='submit' value='Add One To Cart' onClick='getDoc1(\"$itemno\");'/> </form></td></tr>";
$count++;
}
echo "</table>";
if ($count ==0)
echo"No data available";
?>
Updatexml.php:
<?php
$itemnum=$_GET['itemnum'];
$xmlFile = "../../data/Assign/goods.xml";
$doc= DOMDocument::load($xmlFile);
$item = $doc->getElementsByTagName("item");
foreach( $item as $node)
{
$itemno = $node->getElementsByTagName("itemno");
$itemno=$itemno->item(0)->nodeValue;
$qua = $node->getElementsByTagName("QuantityAvailable");
$qua=$qua->item(0)->nodeValue;
$qhold = $node->getElementsByTagName("QuantityHold");
$qhold=$qhold->item(0)->nodeValue;
$unitprice = $node->getElementsByTagName("unitprice");
$unitprice=$unitprice->item(0)->nodeValue;
if($itemno==$itemnum)
{
$itemnoo=$itemno;
$quantity=1;
$price=$unitprice;
$node->getElementsByTagName("QuantityAvailable")->item(0)->nodeValue=$qua-1;
$node->getElementsByTagName("QuantityHold")->item(0)->nodeValue=$qhold+1;
}
}
$doc->save("../../data/Assign/goods.xml");
echo"Shopping Cart";
echo "<table border=1><tr><th>ItemNo</th><th>Quantity</th><th>Price</th><th>Remove from Cart</th></tr>";
echo" <tr><td>{$itemnoo}</td><td>{$quantity}</td><td>{$price}</td><td><form> <input type='button' name='submit' value='Remove from Cart' onClick='getDoc1(\"$itemnoo\");'/> </form></td></tr>";
echo"<tr><td>Total:</td><td> $price </td></tr>";
echo "</table>";
?>
i did some changes in updatexml.php, it now showing shoppingcart but i cant update the values in shopping cart i.e increase the quantity of item by clicking' Add One To cart' button if item is already in a shopping cart. my code is:
<!--file updatexml.php -->
<?php
$itemnum=$_GET['itemnum'];
$xmlFile = "../../data/folder/goods.xml";
$doc= DOMDocument::load($xmlFile);
$item = $doc->getElementsByTagName("item");
foreach( $item as $node)
{
$itemno = $node->getElementsByTagName("itemno");
$itemno=$itemno->item(0)->nodeValue;
$qua = $node->getElementsByTagName("QuantityAvailable");
$qua=$qua->item(0)->nodeValue;
$qhold = $node->getElementsByTagName("QuantityHold");
$qhold=$qhold->item(0)->nodeValue;
if(($itemno==$itemnum)&&($qua>0))
{
$node->getElementsByTagName("QuantityAvailable")->item(0)->nodeValue=$qua-1;
$node->getElementsByTagName("QuantityHold")->item(0)->nodeValue=$qhold+1;
}
}
$doc->save("../../data/folder/goods.xml");
$xmlFile = "../../data/folder/goods.xml";
$doc= DOMDocument::load($xmlFile);
$item = $doc->getElementsByTagName("item");
echo"Shopping Cart";
echo "<table border=1><tr><th>ItemNo</th><th>Quantity</th><th>Price</th><th>Total</th><th>Remove from Cart</th></tr>";
foreach( $item as $node)
{
$itemno = $node->getElementsByTagName("itemno");
$itemno=$itemno->item(0)->nodeValue;
$qua = $node->getElementsByTagName("QuantityAvailable");
$qua=$qua->item(0)->nodeValue;
$qhold = $node->getElementsByTagName("QuantityHold");
$qhold=$qhold->item(0)->nodeValue;
$unitprice = $node->getElementsByTagName("unitprice");
$unitprice=$unitprice->item(0)->nodeValue;
$quantity=1;
if($qhold>0)
{
$total=$unitprice*$quantity;
echo" <tr><td>{$itemno}</td><td>{$quantity}</td><td>{$unitprice}</td><td>{$total}</td><td><form> <input type='button' name='submit' value='Remove From Cart' onClick='getDoc1(\"$itemno\");'/> </form></td></tr>";
}
}
echo"</table>";
?>
In your javascript, this line
document.getElementById("updatexml").innerHTML=xmlhttp.responseText;
means that upon every XMLHttpRequest (every time you add to a cart??) element with id "updatexml" will get updated with the response from updatexml.php in your case with this
echo"Shopping Cart";
echo "<table border=1><tr><th>ItemNo</th><th>Quantity</th><th>Price</th><th>Remove from Cart</th></tr>";
echo" <tr><td>{$itemnoo}</td><td>{$quantity}</td><td>{$price}</td><td><form> <input type='button' name='submit' value='Remove from Cart' onClick='getDoc1(\"$itemnoo\");'/> </form></td></tr>";
echo"<tr><td>Total:</td><td> $price </td></tr>";
echo "</table>";
As you can see you are listing only one item. Once you add another one, the response will override your shopping cart,. So your updatexml.php should list all products**, and look something liek this
<--updatexml.php--->
<?php
$itemnum=$_GET['itemnum'];
$xmlFile = "../../data/Assign/goods.xml";
$doc= DOMDocument::load($xmlFile);
$item = $doc->getElementsByTagName("item");
echo"Shopping Catalogue";
echo "<table border=1><tr><th>ItemNo</th><th>ItemName</th><th>Description</th><th>Price</th><th>Quantity Available</th><th>Add To Cart</th></tr>";
foreach( $item as $node)
{
$itemno = $node->getElementsByTagName("itemno");
$itemno=$itemno->item(0)->nodeValue;
$qua = $node->getElementsByTagName("QuantityAvailable");
$qua=$qua->item(0)->nodeValue;
$qhold = $node->getElementsByTagName("QuantityHold");
$qhold=$qhold->item(0)->nodeValue;
$unitprice = $node->getElementsByTagName("unitprice");
$unitprice=$unitprice->item(0)->nodeValue;
if($itemno==$itemnum)
{
$itemnoo=$itemno;
$quantity=1;
$price=$unitprice;
$node->getElementsByTagName("QuantityAvailable")->item(0)->nodeValue=$qua-1;
$node->getElementsByTagName("QuantityHold")->item(0)->nodeValue=$qhold+1;
}
if($qua>0)
{
echo" <tr><td>{$itemno}</td><td>{$itemname}</td><td>{$itemdes}</td><td>{$price}</td><td>{$quant}</td><td><form> <input type='button' name='submit' value='Add One To Cart' onClick='getDoc1(\"$itemno\");'/> </form></td></tr>";
}
}
$doc->save("../../data/Assign/goods.xml");
echo "</table>";
**Alternatvely you can echo just a row and in your javascript update a table by adding a row on bottom of your table. Also, why don't you use jquery? (then you can play around wih json. Also, It will make your life easier.