猿问

在不同页面上显示复选框输出

如何在不同的 php 页面上显示输出,非常感谢您的帮助,谢谢


<?php

$connect=mysqli_connect("localhost","root","","test");


 $sql = "SELECT * FROM icheck ";

 $result = mysqli_query($connect, $sql);

 //$row = mysqli_fetch_array($result);

?>

//MY FIRST PAGE

<form method="POST">

<table>

    <thead>

        <tr>

            <th>ID</th>

            <th>Name</th>

            <th>Price</th>

            <th>Check</th>

        </tr>

    </thead>

    <?php

    if(mysqli_num_rows($result)>0){

        $i=1;


    while($row=mysqli_fetch_array($result))

    {

echo'<tr>

    <td>'.$row['id'].'</td>

    <td>'.$row['name'].'</td>

    <td>'.$row['price'].'</td>

    <td><input type="checkbox" name="check[]" value='.$row['id'].'></td>


    ';

    $i++;

    }

    }

        ?>


</table>

<input type="submit" name="submit" Value="Submit"/>

</form>


//OUTPUT TO BE DISPLAYED ON OTHER PAGE or 2nd PAGE

<?php 

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

if(!empty($_POST['check'])) {

// Counting number of checked checkboxes.

$checked_count = count($_POST['check']);

echo "You have selected following ".$checked_count." option(s): <br/>";

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

$sum=0;

foreach($_POST['check'] as $selected) {

    $ah="SELECT * FROM icheckWHERE id=".$selected;

    $rowz = mysqli_query($connect, $ah);

    $row=mysqli_fetch_array($rowz);

    echo $row['name']."</br>";

    $test=$row['price'];

    $sum+=(int)$test;


}


echo "Jay Total na ket: ".$sum;

}

}

else{

echo "<b>Please Select Atleast One Option.</b>";

}

}


?>



慕森王
浏览 124回答 2
2回答

侃侃尔雅

您在第一页中的表单不包含表单操作,因此您必须将表单操作放到第二页,然后可以从第二页捕获发布的数据。//我的第一页<form method="POST" action="mysecondpage.php"><table>&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>ID</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Price</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Check</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead>&nbsp; &nbsp; <?php&nbsp; &nbsp; if(mysqli_num_rows($result)>0){&nbsp; &nbsp; &nbsp; &nbsp; $i=1;&nbsp; &nbsp; while($row=mysqli_fetch_array($result))&nbsp; &nbsp; {echo'<tr>&nbsp; &nbsp; <td>'.$row['id'].'</td>&nbsp; &nbsp; <td>'.$row['name'].'</td>&nbsp; &nbsp; <td>'.$row['price'].'</td>&nbsp; &nbsp; <td><input type="checkbox" name="check[]" value='.$row['id'].'></td>&nbsp; &nbsp; ';&nbsp; &nbsp; $i++;&nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?></table><input type="submit" name="submit" Value="Submit"/></form>

慕田峪4524236

起初 vardump 这个$_POST['check']。它是数组吗,您的代码可以工作。通过 vardump 变量,您可以找到解决方案。您可以与我分享您的 vardump 结果。
随时随地看视频慕课网APP
我要回答