[英]how to update DIV content (such as cart DIV)
我使用$.load()
函數加載購物車詳細信息。 但是,當我單擊添加鏈接時,它僅在購物車中顯示具有該特定ID的詳細信息。 似乎每次嘗試添加一項時都會創建購物車。
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>
代替:
<a href="javascript:void(0)" onClick="recp('.$row["id"].')" >
嘗試這個:
<a href="javascript:void(0)" onClick="recp('<?php echo $row["id"];?>')" >
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.