簡體   English   中英

將選中的復選框值從一個頁面傳遞到 php 中的另一個頁面

[英]passing checked checkbox value from one page to another page in php

如何在PHP中將復選框值從一個頁面發送到另一個頁面? 復選框值是從數據庫中獲取的。 我需要將選中的值從一頁發送到另一頁。 我是 php 新手。

頁面1.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>

試試這個。 它會為你工作

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

此代碼絕對有效:-

創建第一頁- 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

您正在傳遞多個復選框值,而不是一個。 這意味着您的 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM