Php 购物车问题
我有一个问题,在建立一个购物车从目录每当我输入一个新的项目,以前的项目是由新的取代 在下面给出的代码中,我有一个xml文件goods.xml。在showxml.php中,我显示quantityAvailable>0的项目。然后通过getDoc1,我更新了xmlfile,还想更新购物车。问题是,当我选择一个新项目时,以前的项目会被新项目替换。请帮帮我。提前谢谢 我的代码如下 XML文件:goods.XMLPhp 购物车问题,php,xml,ajax,domdocument,Php,Xml,Ajax,Domdocument,我有一个问题,在建立一个购物车从目录每当我输入一个新的项目,以前的项目是由新的取代 在下面给出的代码中,我有一个xml文件goods.xml。在showxml.php中,我显示quantityAvailable>0的项目。然后通过getDoc1,我更新了xmlfile,还想更新购物车。问题是,当我选择一个新项目时,以前的项目会被新项目替换。请帮帮我。提前谢谢 我的代码如下 XML文件:goods.XML <?xml version="1.0" encoding="utf-8"?> &
<?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>
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";
?>
在javascript中,这一行
document.getElementById("updatexml").innerHTML=xmlhttp.responseText;
这意味着在每个XMLHttpRequest(每次您添加到购物车???)上,id为“updatexml”的元素都将使用updatexml.php的响应进行更新
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>";
echo“购物车”;
回显“ItemNoQuantityPriceRemove from Cart”;
回显“{$itemnoo}{$quantity}{$price}”;
echo“总价:$price”;
回声“;
正如您所看到的,您只列出了一项。添加另一个后,响应将覆盖您的购物车,。
所以你的updatexml.php应该列出所有产品**,并且看起来像这样
<--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>";
对你的代码进行格式化,将帮助你更容易地发现问题。u plz能否提供代码(在你的javascript中,这一行交替地,你可以只回显一行,在你的javascript中,通过在表的底部添加一行来更新表)仍然存在同样的问题..我想我在javascript中有问题,请告诉我正确的答案solution@carina有什么问题?它仍然覆盖输出?没有。但是我无法更新购物车,即如果商品已经在购物车中,则将数量增加1。您好,有人回答这个问题吗
document.getElementById("updatexml").innerHTML=xmlhttp.responseText;
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>";
<--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>";