2013-04-23 37 views
0

我用$.load()函數來加載購物車詳細信息。但是,當我點擊添加鏈接時,它只會在我的購物車中顯示具體ID的詳細信息。而且似乎每次嘗試添加一個項目時都會創建購物車。如何更新DIV內容(如購物車DIV)

Code for add button: 
<a href="javascript:void(0)" onClick="recp('.$row["id"].')" > 

腳本:

function recp(id) { 
$('.bottomcorners').load('addToCart.php?id=' + id); 
} 

</script> 

AddToCart function 
$conn = mysql_connect ('localhost','root','') or die("Unable to connect to mysql database server"); 
mysql_select_db('itech7602', $conn) or die ("Unable to select database"); 
$id = $_GET['id']; 

echo $id; 
//Check if the ID variable is set 
if(isset($id)) 
{ 

    //Escape the string from the URL 
    $ID = mysql_real_escape_string($id); 

    //Check if the ID passed exists within the database 
    $result = mysql_query('SELECT * FROM product WHERE id = "'.$ID.'" LIMIT 1'); 

    //Get the total results of if any product matched the query 
    $totalRows = mysql_num_rows($result); 

    //If the product ID exists in the database then insert it to the cart 
    if($totalRows > 0) 
    { 

     while($row = mysql_fetch_assoc($result)) 
     { 

      //Check if the cart exists 
      if(cartExists()) 
      { 

       //The cart exists so just add it to the cart 
       insertToCart($ID, $row['name'], $row['price']); 

      } 
      else 
      { 

       //The cart doesn't exist so create the cart 
       createCart(); 

       //The cart is now created so add the product to the cart 
       insertToCart($ID, $row['name'], $row['price']); 

      } 

     } 

    } 
    else 
    { 

     //No products were found in the database so notify the user, redirect him and stop the code from continuing 
     //notify('Sorry but there is no product with that ID.', 0); 
     echo ("No item with that ID"); 
     //header('Location: store.php'); 

    } 

    //The product was successfully added so set the notification and redirect to the cart page 
    //notify('Product added to the cart.', 1); 
    echo ("Product added to the cart."); 
    //header('Location: store.php'); 

} 
else 
{ 

    //No Product with that ID redirect and display message 
    //notify('Sorry but there is no product with that ID.', 0); 
    echo "Add Failed!"; 
    //header('Location: store.php'); 

} 
getCart(); 
exit(); 
?> 

像insertToCart,getCart()函數都在一個名爲functions.php的另一個PHP腳本聲明。

有什麼建議嗎?

我試圖用InnerHTML,但沒有得到運氣

功能addPro(ID){

// This will create the XmlHttpRequest in modern browsers 
if(window.XMLHttpRequest){ 
    xmlhttp=new XMLHttpRequest();     
} 
// This will create the XmlHttpRequest in older versions of Internet Explorer 
else{ 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 

// This is how you tell the script what to do with the response from the request 
xmlhttp.onreadystatechange=function() 
{ 
    if(xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
     document.getElementById("bottomcorners").innerHTML=xmlhttp.responseText; 
    } 
} 
xmlhttp.open("GET", "addToCart.php?id="+id, true); 
xmlhttp.send(); 
} 
</script> 
+0

pl輕鬆把代碼放在'cartExists()' – Amir 2013-04-23 06:28:16

回答

0

相反的:

<a href="javascript:void(0)" onClick="recp('.$row["id"].')" > 

試試這個:

<a href="javascript:void(0)" onClick="recp('<?php echo $row["id"];?>')" >