0

我使用$.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>
4

1 回答 1

0

代替:

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

尝试这个:

<a href="javascript:void(0)" onClick="recp('<?php echo $row["id"];?>')" >
于 2013-04-23T06:25:54.267 回答