如何更新DIV内容(例如购物车DIV)

时间:2013-04-23 06:13:29

标签: php javascript

我使用$.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,但没有运气

   function 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>

1 个答案:

答案 0 :(得分:0)

而不是:

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

试试这个:

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