购物车问题

时间:2011-04-18 10:06:36

标签: php xml ajax domdocument

我在从目录中构建购物车时遇到问题每当我输入新项目时,前一项将被新项替换。

在下面给出的代码中,我有一个xml文件goods.xml。在showxml.php中,我显示quantityAvailable> 0的项目。然后通过getDoc1,我更新了xmlfile,我也想更新购物车。问题是,当我选择一个新项目时,前一个项目将替换为新项目。请帮我。提前致谢

我的代码如下:

XML文件: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函数:

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>";
?>

我在updatexml.php上做了一些更改,它现在显示了shoppingcart但我无法更新购物车中的值,即如果商品已经在购物车中,点击“添加一个购物车”按钮可以增加商品数量。 我的代码是:

<!--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>";

?>    

1 个答案:

答案 0 :(得分:0)

在你的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>";

如您所见,您只列出了一个项目。添加另一个后,响应将覆盖您的购物车。 所以你的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>";

**另外,您可以回显一行,并在您的javascript中通过在表格底部添加一行来更新表格。另外,你为什么不使用jquery? (然后你可以和json一起玩。而且,它会让你的生活更轻松。

相关问题