【问题标题】:how to get value from radio button in php如何从php中的单选按钮获取值
【发布时间】:2018-05-04 21:35:44
【问题描述】:

我有一张桌子,在这张桌子上我有一列叫做状态。在状态列下,我有两个并排的单选按钮,一个已准备好,另一个未准备好,现在我想从这些单选按钮中获取每一行中的任何一个值并将该值存储到数据库中。但它只捕获了我从它们中选择的第一行单选按钮值,然后将值复制到每一行并存储到数据库中。下面是我的代码..

HTML 代码:

<form action = "submit_varification.php" method = "POST" onclick = "return validate()">
                            <div style="position: absolute; left: 50px; top: 90px;">
                            <label class="right"><font color="white">Date:</font></label>
                            <input type="text" id = "frmDate"  /><br>
                            <p id="date"></p>
                            </div>
                            <div style="position: absolute; left: 250px; top: 91px;">
                            <label class="right"><font color="white">V-ID:</font></label>
                            <input type="text" id = "myText" name = "reviewer" value = ""/><br>
                            </div>
                            <div style="position: absolute; left: 900px; top: 91px;">
                            <button type="button" name="show" id="show" onclick = "" >History</button>
                            </div>
                            <div style="position: absolute; left: 900px;">
                            <input type="submit" name="test" id="test" value="Submit"  /><br/>
                            </div>

                        <script>
                        var date = new Date();

                        document.getElementById("frmDate").value = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();

                        </script>
                    </body>
                    <table style="position: absolute; width:95%; left: 20px; top: 150px;" id = "table1">
                        <?php
                        $myDate = date('m/d/Y');
                        ?>
                            <tr>
                            <th>Date</th>
                            <th>Time</th> 
                            <th>Alias</th>
                            <th>Machine_Name</th>
                            <th>Build_Name</th>
                            <th>Build_version</th>
                            <th>WinDBG</th>
                            <th>.Net_Framework</th>
                            <th>Status</th>
                            </tr>
                            <?php
                                //get records from database
                                $sql3 = "SELECT * FROM data WHERE `Date` = '".$myDate."' ORDER BY id DESC";
                                $query = $conn->query($sql3);
                                if($query->num_rows > 0){
                                while($row = $query->fetch_assoc()){ ?>
                                <tr> 
                                <td><?php echo $row['Date']; ?></td>
                                <td><?php echo $row['Time']; ?></td>
                                <td><?php echo $row['Alias']; ?></td>
                                <td><?php echo $row['Machine_Name']; ?></td>
                                <td><?php echo $row['Build_Name']; ?></td>
                                <td><?php echo $row['Build_Version']; ?></td>
                                <td><?php echo $row['WinDBG']; ?></td>
                                <td><?php echo $row['.NET_Framework']; ?></td>
                                <td style='white-space: nowrap'><form><label class = "ready"><input type="radio" name="status" value = "Ready">Ready</label><label class = "notready"><input type="radio" name="status" value = "Not Ready" >Not Ready</label></form></td>
                                </tr> 
                                <?php } } ?>

                            </table>
                            </form>

PHP 代码:

<?php

                            session_start();
                            $myDate = date('m/d/Y');
                            $servername = "localhost";
                            $username = "root";
                            $password = "wipro@123";
                            $dbname = "testbad_information";
                            $date = date("m/d/Y");

                            // Create connection
                            $conn = new mysqli($servername, $username, $password, $dbname);
                            $sql4 = "SELECT * FROM data WHERE `Date` = '".$myDate."' ORDER BY id DESC";
                            $reviewer = $_POST['reviewer'];
                            $status = $_POST['status'];

                            $query = $conn->query($sql4);
                            if($query->num_rows > 0){
                            while($row = $query->fetch_assoc()){


                                //...insert into your DB.
                                $row1 = $row["Date"];
                            $row2 = $row["Time"];
                            $row3 = $row["Alias"];
                            $row4 = $row["Machine_Name"];
                            $row5 = $row["Build_Name"];
                            $row6 = $row['Build_Version'];
                            $row7 = $row["WinDBG"];
                            $row8 = $row[".NET_Framework"];

                            $sql5 = "INSERT INTO history (`Date`, `Time`, `Alias`, `Machine_Name`, `Build_Name`, `Build_version`, `WinDBG`, `.NET_Framework`, `Status`, `Reviewed_By`) 
                            VALUES ('".$row1."','".$row2."','".$row3."','".$row4."','".$row5."','".$row6."','".$row7."','".$row8."','".$status."','".$reviewer."') ";
                            if ($conn->query($sql5) === TRUE) {
                            //echo "New record created successfully";
                            //echo nl2br("\n");
                            echo "";

                            } else {
                            echo "Error: " . $sql5 . "<br>" . $conn->error;
                            }



                        } 
                    }

                     include 'popupmessage.html';

                 ?>

请帮帮我……!!我想存储每一行​​的状态。 喜欢 第 1 行 |准备好 第 2 行 |没有准备好 但现在它对所有行都给出了相同的值。

【问题讨论】:

标签: php html database radio-button


【解决方案1】:

在 OP 中,每个数据行的所有单选按钮都具有相同的名称。每行都需要一个标识符。将数据行 ID 放入无线电名称会有所帮助。在 PHP 中循环遍历每一行以借助数据行 ID 读取单选按钮的值。

【讨论】:

  • 请举个例子...!!
  • @KunalMajumder 请先尝试一下。我不会为你重写代码。
猜你喜欢
  • 2018-10-07
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多