2017-07-07 24 views
1

這是更新頁面,顯示客戶購物籃內容。購物細節保存在'mcart'表中。 它由mcartId,mcookieId,mpr,mqty,mpn & des字段組成。 主要部件號,價格,描述&價格折扣取自表2。 典型的更新頁面看起來這樣:使用javascript混合php.Toggle顯示/隱藏獲取的數據

p-n price qty remove? qty discounts 

s-12 10.25 1 remove Items 

b-12 3.64 1 remove Items 

'Items' contains discount range which is fetched from another table      e.g. 
Items: 
1-5 ,0% 

6-19 ,12% 

20-39 ,25% 

40-59 ,33% 

60-99 ,37% 

100-199 ,42% 

200-499 ,45% 

500-9999 ,48% 

這些貼現率是隱藏的,將被揭示一旦用戶點擊了「項目」來切換隱藏或顯現。 例如,如果客戶購買1-5件商品的價格將是2.50英鎊,但是6-9英鎊將減少到1.90英鎊等等。 折扣範圍是隱藏的,理想情況下應該在'項目'下面顯示它所點擊的任何項目。 目前它只顯示第一個項目的折扣,如我所料,不管我選擇哪個項目。 如何更改JavaScript,以便它能記住我點擊了哪個項目並僅顯示該項目的折扣百分比? 請幫忙!

<script type="text/javascript"> 

    function toggle(id){ 
    var e=document.getElementById(id); 
    if (e.style.display == '') 
    { 
    e.style.display = 'none'; 
} 
else { 
e.style.display = ''; 
} 
} 
</script> 

    <?php 


//  ====== Connection to database ============================== 
include("order/connection.php");  


// ============== identify if item has been removed or qty changed=> whats the part number? ========= 

$id = $_GET[id]; 

$mqty = $_POST[chmqty]; 

$mpn = $_POST[mmpn]; 



    //  ================If qty has been changed ============== 

    if (isset($_POST['chmqty'])) 
     { 

$stmt = $pd->prepare('SELECT * FROM table2 WHERE 
    part_number=:part_number' ); 
$stmt->execute(array(':part_number' => $mpn)); 
$row = $stmt->fetch(PDO::FETCH_BOTH); 



    //  ====== Get the correct price break ===================== 

    for($in =1 ; $in <= 8; $in++) 


    { 
          $bb=$row["price_break".($in)]; 
          $halves =explode("-",$bb); 
          $firstnumber=$halves[0]; 
          $secondnumber=$halves[1]; 


           If ($mqty >= $firstnumber && $mqty <= 
$secondnumber) 
           { 

           $price= 
number_format($row[("price_each".$in)], 2, ".", ","); 


           } 
     } 
//  ================================ 


$query = "UPDATE mcart SET mqty='$mqty', mpr='$price' 

WHERE mcookieId = :cookie AND mpn= :part"; 
$stmt3=$pd->prepare($query); 
$stmt3->BindValue(':cookie',$_COOKIE[mcartId], PDO::PARAM_STR); 
$stmt3->BindValue(':part',$_POST[mmpn], PDO::PARAM_STR); 

    $stmt3->execute(); 

} 




// =============== If DELETE button has been pressed ====== 


if (!empty($id)) 
{ 

$statement2= 'DELETE FROM mcart WHERE mcookieId=? AND mpn=?'; 

$stmt1 = $pd->prepare($statement2); 

$stmt1->execute(array($_COOKIE[mcartId],$id)); 

} 




// ================= Display customer Shopping Basket ========== 

$statement= "SELECT * FROM mcart WHERE mcookieId=:cookie"; 

$stmt2 = $pd->prepare($statement); 

$stmt2->bindParam(':cookie', $_COOKIE[mcartId], PDO::PARAM_STR); 

$stmt2->execute(); 


?> 



<Table class="tupdate"> 
<tr > 
<th class="pn"> p-n 
</th> 



<th class="pr"> price 
</th> 


<th class="qty"> qty 
    </th> 



    <th class="remove"> remove? 
    </th> 


<th class="disc">discounts 
    </th> 


</tr> 


<?php 

while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) 
{ 
echo "<tr class='basket1'>"; 



// ================ Show Part Numbers =============== 
echo "<td class='basket1'>"; 
echo $row['mpn']; 
echo "</td>"; 





    // ================ Show Proces =============== 

    echo "<td class='basket1'>"; 
    echo $row['mpr']; 
    echo "</td>"; 

    echo "<form method='POST' action='".$_SERVER['PHP_SELF']."'>"; 


    // ===== Show Qty already in shopping basket that can be changed, 
    // =====get qty & partnumber if update is clicked ====== 
    echo "<td class='basket1'>"; 

echo "<input type='number' size='3' value='".$row['mqty']."'  
name='chmqty' class='chmqty'>" ; 


echo "<input type='hidden' name='mmpn' value='".$row['mpn']."'>"; 

echo" <Input type='submit' value='update'>"; 

echo "</form>"; 
echo "</td>"; 


    // == An item can be removed from basket, get the part number === 
echo "<td class='basket1'>"; 


echo "<a href='update1.php?id=" 

.$row['mpn']. 

"'>remove</a>"; 

echo "</td>"; 

    // ===== Show the price break range and associated discount 
percentage from Table2 ==== 
echo "<td class='basket1'>"; 
?> 

<a href="#" onclick="toggle('objDetails')">Items</a> 
    <span id="objDetails" style="display:none"> 



    <?php 

    //  ====== calculate how many times in basket and the total 
price so far ===== 

    $totq=$row["mqty"]; 


    $totqty=$totqty+$totq; 


    $totp=$row["mqty"]*$row["mpr"]; 
    $totpr=$totpr+$totp; 



    //  ====== Connect to Table 2, Find the relevant p-n and its 
discount percentage and lis it ========= 


$stmt = $pd->prepare("SELECT * FROM table2 LEFT JOIN mcart ON 
table2.part_number = mcart.mpn WHERE table2.part_number 
=:part_number"); 


$stmt->execute(array(':part_number' => $row['mpn'])); 

$row = $stmt->fetch(PDO::FETCH_BOTH); 




     $c=$row["price_each1"];// price for single item 


          for($i = 1; $i <= 8; $i++) 
          { 

          $b=$row["price_each".$i]; 


          if ($b !=0.00) 
           { 

           $d=(($c-$b)/$c)*100; 

      $complete=$row[("price_break".$i)]. " ," .round($d)."%"; 


          echo"</br>"; 
          echo $complete; 
           echo"</br>"; 

            } 
           } 


           echo "</span>"; 


    echo "</td>"; 

    //} 


    } 
?> 
</tr> 
<table > 

<tr > 

<!-- <td ><?php //echo "Total purchases: ".$total."for part 
number".$_POST["mpn"];?> </td> --> 
<th class="basket2"> </th> 
<th class="basket1"><?php echo "Total of &pound;".$totpr;?> </th> 

<th colspan="2" class="basket2"><?php echo "for ".$totqty." items";?> 
</th> 
    <th class="basket3"> <img src="/stampede/images/scart.jpg" 
alt="Shopping Cart" width="20"> </th> 
    </tr> 

</table> 

<?php 
echo "</br>"; 
echo "</br>"; 


include ("order/options1.php"); 
?> 
+0

添加一些你的HTML代碼,請 –

+0

任何人都可以請幫忙! – jack

回答

0

已解決。您必須通過URL字符串將變量傳遞到新頁面。 我用下面的例子:

INSIDE 「page1.php中」 或 「page1.html」

// Send the variables myNumber=1 and myFruit="orange" to the new PHP page... 
<a href="page2c.php?myNumber=1&myFruit=orange">Send variables via URL!</a> 

INSIDE 「page2c.php」

<?php 
    // Retrieve the URL variables (using PHP). 
    $num = $_GET['myNumber']; 
    $fruit = $_GET['myFruit']; 
    echo "Number: ".$num." Fruit: ".$fruit; 
?>