簡體   English   中英

我不知道我必須將javascript代碼放在php代碼的哪一行

[英]I don't know which line that I have to put the javascript code in the php code

我有一個添加到購物車按鈕。

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']?>)" />

這是“添加”的過程:

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;
 }

任何幫助/建議將不勝感激。 謝謝。

我猜它將在addtocart函數內部:

if(product_exists($pid)){
   echo '<script type="text/javascript">alert("This item is already in the  cart.");window.location.href="cart.php";</script>';
   return;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM