将已检查的复选框值从一个页面传递到另一个页面

时间:2016-03-16 05:21:53

标签: php html sql

如何在PHP中将复选框值从一个页面发送到另一个页面?复选框值是从数据库中获取的。我需要将检查的值从一个页面发送到另一个页面。我是php的新手。

page1.php中

<form method="post" action="setupstates.php">
  <table border=".3px" id='acttable'>
    <tr>
      <td><b>Act Name</b></td>
      <td colspan="5"><input type='text' name="actname" size="130" required></td>
    </tr>
    <tr>
      <td><b>Industry Type</b></td>
      <td colspan="5"><input type="checkbox">
        Select All Industries</td>
      <?php
                $conn = get_dbconnect();
                $sql1 = "SELECT*FROM industry";
                $result = mysqli_query($conn, $sql1);
                $nrows = mysqli_num_rows($result);
                //echo $nrows;

                $sql = "SELECT name FROM industry"; //selection query
                $rs = mysqli_query($conn, $sql); //odbc_exec($conn,$sql);
                // if(mysqli_num_rows($rs)>0){
                while ($nrows > 0) {
                    echo "<tr>";
                    for ($i = 0; $i < 5; $i++) {
                        $row = mysqli_fetch_assoc($rs);
                        echo "<td><input type='checkbox' name='industrytype' value='" . $row['name'] . "'/>" . $row['name'];
                    }
                    $nrows = $nrows - 5;
                    echo '</tr>';
                }
                ?>
    </tr>
  </table>
  <input type="submit">
</form>

page2.php(setupstates.php)

<table>
  <tr>
    <td><b>Industry Type</b></td>
    <td><input type="checkbox" value="<?php print $_POST["industrytype"] ?>"></td>
  </tr>
</table>

3 个答案:

答案 0 :(得分:1)

试试这个。它适合你

 page1.php

    <form method="post" action="setupstates.php">
      <table border=".3px" id='acttable'>
        <tr>
          <td><b>Act Name</b></td>
          <td colspan="5"><input type='text' name="actname" size="130" required></td>
        </tr>
        <tr>
          <td><b>Industry Type</b></td>
          <td colspan="5"><input type="checkbox">
            Select All Industries</td>
          <?php
                    $conn = get_dbconnect();
                    $sql1 = "SELECT*FROM industry";
                    $result = mysqli_query($conn, $sql1);
                    $nrows = mysqli_num_rows($result);
                    //echo $nrows;

                    $sql = "SELECT name FROM industry"; //selection query
                    $rs = mysqli_query($conn, $sql); //odbc_exec($conn,$sql);
                    // if(mysqli_num_rows($rs)>0){
                    while ($nrows > 0) {
                        echo "<tr>";
                        for ($i = 0; $i < 5; $i++) {
                            $row = mysqli_fetch_assoc($rs);
                            echo "<td><input type='checkbox' name='industrytype[]' value='" . $row['name'] . "'/>" . $row['name'];
                        }
                        $nrows = $nrows - 5;
                        echo '</tr>';
                    }
                    ?>
        </tr>
    <tr><td colspan="5"><input type="submit" name="submit" value="submit"></td></tr>
      </table>
      <input type="submit">
    </form>


setupstates.php


<?php

if($_POST){
$chk = array();
$chk = $_POST['industrytype'];
$total = count($chk);
for($i=0; $i<$total; $i++){?>
<input type="checkbox" checked="checked" value="<?php echo $chk[$i] ?>"><?php echo $chk[$i]?>

<?php}}?>

答案 1 :(得分:0)

此代码绝对有效:-

创建首页index.php

<div>
    form action="process.php" method="post">
    <label>Select your interest :</label>
    <input type="checkbox" name="course[]" value="Java"> Java
    <input type="checkbox" name="course[]" value="Php"> PHP
    <input type="checkbox" name="course[]" value="Python"> Python
    <input type="checkbox" name="course[]" value="JavaScript"> JavaScript
</div>
<div>
    <button type="submit">Submit</button>
</div>

创建第二页-Process.php

<?php
$course = (isset($_POST['course'])) ? $_POST['course'] : array(); ?>
<p><strong>Courses :</strong>
    <?php
    if (count($course) > 0) {
        foreach ($course as $course) {
            echo $skill . ' ';
        }
    } else {
        echo "No course has been selected";
    }
    ?>
</p> 

输出:-课程:Java PHP Python JavaScript

答案 2 :(得分:-1)

您传递的是多个复选框值,而不是一个。这意味着你的setupdstates.php需要是一个处理所有值的循环。

请注意,复选框很奇怪。如果你不检查它,没有任何东西被发送到PHP。所以你只会得到那些经过检查的。

<?php //page1.php ?>
<form method="post" action="setupstates.php">
    <table border=".3px" id='acttable'>
    <tr>
        <td><b>Act Name</b></td>
        <td colspan="5"><input type='text' name="actname" size="130" required></td>

    </tr>
    <tr>
        <td><b>Industry Type</b></td>
        <td colspan="5"><input type="checkbox">Select All Industries</td>
        <?php 
            $conn = get_dbconnect();
            $sql1 = "SELECT * FROM industry";
            $result = mysqli_query($conn, $sql1);
            $nrows = mysqli_num_rows($result);
            //echo $nrows;

            $sql= "SELECT name FROM industry"; //selection query
            $rs = mysqli_query($conn, $sql);//odbc_exec($conn,$sql);
            // if(mysqli_num_rows($rs)>0){
            $n = 0;
            while( $nrows > 0 ) {
                echo "<tr>";
                for($i=0; $i < 5; $i++) {
                $row = mysqli_fetch_assoc($rs); 
                    // 
                    echo "<td><input type='checkbox' name='industrytype[]' value='".$row['name']."' onclick=checkall();/>".$row['name'] . "</td>";
                }
                $nrows = $nrows-5;
                echo '</tr>';
            }    
        ?>
    </tr>
    </table>

    <?php // setupstates.php ?>
    <table>
        <?php foreach ($_POST["industrytype"] as $value) : ?>
        <tr>
            <td><b>Industry Type</b></td>
            <td>
                <input type="checkbox" value="<?php print $value; ?>">
            </td>
        </tr>
        <?php endforeach; ?>
    </table>