混合使用php和javascript。切换显示/隐藏获取的数据

混合使用php和javascript。切换显示/隐藏获取的数据,javascript,php,jquery,pdo,Javascript,Php,Jquery,Pdo,这是更新页面,其中显示客户购物篮内容。购物详情保存在“mcart”表中。 它由mcartId、mcookieId、mpr、mqty、mpn和des字段组成。 主要零件编号、价格、说明和价格折扣取自表2。 典型的更新页面如下所示: p-n price qty remove? qty discounts s-12 10.25 1 remove Items b-12 3.64 1 remove Items 'Items' contains discount

这是更新页面,其中显示客户购物篮内容。购物详情保存在“mcart”表中。 它由mcartId、mcookieId、mpr、mqty、mpn和des字段组成。 主要零件编号、价格、说明和价格折扣取自表2。 典型的更新页面如下所示:

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");
 ?>

功能切换(id){
var e=document.getElementById(id);
如果(e.style.display=='')
{
e、 style.display='none';
} 
否则{
e、 style.display='';
}
}

解决了。您必须通过URL字符串将变量传递到新页面。 我使用了下面的例子:

在“page1.php”或“page1.html”中

//将变量myNumber=1和myFruit=“orange”发送到新的PHP页面。。。
在“page2c.php”中


已解决。您必须通过URL字符串将变量传递到新页面。 我使用了下面的例子:

在“page1.php”或“page1.html”中

//将变量myNumber=1和myFruit=“orange”发送到新的PHP页面。。。
在“page2c.php”中



请添加一些html代码,任何人都可以,请帮助!请添加一些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> 
<?php
    // Retrieve the URL variables (using PHP).
    $num = $_GET['myNumber'];
    $fruit = $_GET['myFruit'];
    echo "Number: ".$num."  Fruit: ".$fruit;
?>