【发布时间】:2017-07-25 09:35:26
【问题描述】:
我有一个添加到购物车按钮。
Javacript 代码:
echo '<script type="text/javascript">alert("This item is already in the
cart.");window.location.href="shoppingcart.php";</script>';
如果商品已经在购物车中,我希望显示 javascript 代码。 现在,我想问的是我必须将javascript放在下面的代码中的哪一行??
这里是html代码:
<input type="button" value="Tambah ke Senarai" onclick="addtocart(<?php echo
$row['no']?>)" />
这里是'add'的过程:
if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
$pid=$_REQUEST['productid'];
addtocart($pid,1);
header("location:shoppingcart.php");
exit();
}
<script language="javascript">
function addtocart(pid){
document.form1.productid.value=pid;
document.form1.command.value='add';
document.form1.submit();
}
</script>
这是 addtocart 功能:
function addtocart($pid,$q){
if($pid<1 or $q<1) return;
if(is_array($_SESSION['cart'])){
if(product_exists($pid))return;
$max=count($_SESSION['cart']);
$_SESSION['cart'][$max]['productid']=$pid;
$_SESSION['cart'][$max]['qty']=$q;
}
else{
$_SESSION['cart']=array();
$_SESSION['cart'][0]['productid']=$pid;
$_SESSION['cart'][0]['qty']=$q;
}
}
这是 product_exists 函数:
function product_exists($pid){
$pid=intval($pid);
$max=count($_SESSION['cart']);
$flag=0;
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['cart'][$i]['productid']){
$flag=1;
break;
}
}
return $flag;
}
任何帮助/建议将不胜感激。谢谢。
【问题讨论】:
标签: javascript php jquery