【问题标题】:I don't know which line that I have to put the javascript code in the php code我不知道我必须将javascript代码放在php代码中的哪一行
【发布时间】: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


    【解决方案1】:

    我猜它会进入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;
    }
    

    【讨论】:

    • 那你的条件不匹配,试试exit()而不是return
    • 我只是意识到,即使我使用了 exit(),我也无法在使用您的答案更改购物车后将任何商品添加到购物车中。
    猜你喜欢
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 2021-07-31
    • 2011-08-30
    相关资源
    最近更新 更多