関数を使用$.load()
してカートの詳細をロードしました。ただし、追加リンクをクリックすると、ショッピング カートにその特定の ID を持つ詳細のみが表示されます。そして、商品を1つ追加しようとすると毎回ショッピングカートが作成されるようです。
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>