簡體   English   中英

在同一表單上顯示基於Checkbox的數據庫中的值

[英]display Value from database based on Checkbox checked on the same form

我正在嘗試根據選中的復選框填充數據庫中名字的值。

下面是表單,其中填充了數據庫中添加的所有表數據。 通過選中復選框中的任何一個並單擊更新,它將顯示一個警報,其中顯示從該行ID的數據庫中查詢的名字,然后重定向到另一頁

我嘗試使用method = get in form。

現在,我被困在如何解決以下錯誤上:

<Undefined index: users on line 98>

這是我的代碼

<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
<head>
   <style>
        html {
            background-image: url("WDF_2493417.jpg");
            background-size: 100% 100%;
            background-repeat: no-repeat;
            background-position: center;
        }

        html {
            margin: 0;
            padding: 0;
            height: 100%;
        }
    </style>
</head>
<body>
<?php
    // php populate html table from mysql database
    $conn = mysqli_connect("localhost","root","");
    mysqli_select_db($conn, "my_db");
    $result = mysqli_query($conn,"SELECT * FROM ijob ORDER BY userId DESC");
?>

    <div class="form-style-1">
        <form name="frmUser" method="post" action="">
            <div style="width:1200px;">
                <table border="0" cellpadding="10" cellspacing="1" width="1200" class="tblListForm" align="center">
                    <tr class="listheader">
                        <td></td>
                        <th>first name</th>
                        <th>last name</th>
                        <th></th>
                    </tr>
                    <?php
                        $i=0;
                        $j=1;
                        while($row = mysqli_fetch_array($result)) {
                            if($i%2==0){
                                $classname="evenRow";
                            } else {
                                 $classname="oddRow";
                            }
                    ?>
                            <tr class="<?php if(isset($classname)) { echo $classname; }?>">
                                <td>
                                    <input type="checkbox" name="users" value="<?php echo $row["userId"]; ?>" >
                                </td>
                                <td><?php echo $row["fname"]; ?></td>
                                <td><?php echo $row["lname"]; ?></td>
                                <th colspan="4">
                                    <input type="button" name="update" class="button" value="Apply" onClick="checkAddress(this);" />
                                </th>
                            </tr>
                    <?php 
                            $i++;
                            $j=$i+1;
                        }
                        $conn->close();
                    ?>
                </table>
            </div>
        </form>
    </div>
</body>
<script>
    function checkAddress()
     {
        if (document.querySelector('form[name="frmUser"] input[type="checkbox"]:checked')) {

            if(!empty($_POST['users'])){
            <?php
                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "my_db";

                // Create connection
                $conn = new mysqli($servername, $username, $password, $dbname);
                // Check connection
                if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                } 
                $num_var= " ";
                $num_var = mysqli_real_escape_string($conn,$_POST['users']); // getting error on this Line.

                $query = "SELECT fname FROM ijob WHERE userId='$num_var'";
                $result = mysqli_query($conn,$query);
                if (!$result) {
                    echo 'MySQL Error: ' . mysqli_error($conn);
                    exit;
                }

                if ($result->num_rows > 0) {
                    // output data of each row
                    while($row = $result->fetch_assoc()) {
                        $fnames[] = $row['fname'];
            ?>
            alert("Hello <?php echo $fnames;?>");
            <?php
                    }
                }
                $conn->close();

            ?>
            }

            document.frmUser.action = "form.php";
            document.frmUser.submit();
        } else {
            alert("Pl select checkbox which You wish to Apply ");
        }
    }
</script>
</html>

您的<script></script>中的PHP開頭和結尾標記不在正確的位置。 1.在此if語句后,您有一個PHP開頭標記( <?php

if(!empty($_POST['users'])){
   <?php

應該在if語句之前

<?php
if(!empty($_POST['users'])){
  1. 您的PHP結束標記( ?> )在最后一個}之前。

    ?>
    }

暫無
暫無

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

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