2017-03-05 1 views
0

私はカートに入れるボタンがあります。JavaScriptコードをJavaScriptコードに入れなければならない行はわかりません

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

ご協力いただきありがとうございます。ありがとうございました。

答えて

0

それは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; 
} 
+0

それは働いていません。 javascriptは表示されません。 –

+0

あなたの条件が一致しない場合、返品の代わりに 'exit()'を試してください – meda

+0

私はexit()を使ってもあなたの答えを使って変更した後、カートにアイテムを追加できません。 –

関連する問題