<input type =“ submit” />与php之间的通信

时间:2018-12-05 17:06:07

标签: php html forms

请参考图片。我想按下按钮的值,这是数据库中学生的ID。我怎么知道用户按下了哪个按钮? 问题在于所有输入的名称都相同。

                <form method="post" name="data">
                    <table style="overflow-x: auto; display: block; white-space: nowrap; max-height: 450px; max-width: 1100px;"
                           class="table table-striped">
                        <tr>
                            <th>ID</th>

                            <th>Student Name</th>
                            <th>Address</th>
                            <th>Contact No.</th>
                            <th>Email</th>
                            <th colspan="4">Accept / Reject</th>
                        </tr>
                        <?php
                        $query = "SELECT StudentID,StudentName,Address,ContactNo,Email FROM Student WHERE Accepted=0";
                        $mysqli = connect();
                        //Get result
                        $result = $mysqli->query($query) or die($mysqli->error . " " . __LINE__); //__LINE__ shows the line no. we are getting the error at

                        if ($result->num_rows > 0) {
                            //Loop through results
                            while ($row = mysqli_fetch_array($result)) {
                                //Display customer info
                                $output = "<tr>";
                                $output .= "<td>" . $row['StudentID'] . "</td>";

                                $output .= "<td>" . $row['StudentName'] . "</td>";
                                $output .= "<td >" . $row['Address'] . "</td>";
                                $output .= "<td >" . $row['ContactNo'] . "</td>";
                                $output .= "<td >" . $row['Email'] . "</td>";

                                $output .= "<td><input type='hidden'  name='Yes" . $row['StudentID'] . "' value='" . $row['StudentID'] . "' ></td>";
                                $output .= "<td><input type='submit'   value='yes' class='btn-success' style='border:none;'></td>";

                                $output .= "<td><input type='hidden'  name='No' value='" . $row['StudentID'] . "' ></td>";
                                $output .= "<td><input type='submit'  value='No' class='btn-link' style='border:none;' ></td>";;
                                $output .= "</tr>";
                                echo $output;
                            }
                            echo "</table>";

                        } else
                            echo "No pending request found";

                        ?>
                    </table>
                </form>

4 个答案:

答案 0 :(得分:1)

如果您有多个提交按钮(foreach的每个循环一个),那么您需要为按钮传递唯一的value来标识您按下了哪个按钮。一种简单的方法是同时传递是/否答案和studentID。例如:

<input type="submit" name="response" value="yes|<?php echo $row['studentID']; ?>" class="btn-success" style="border:none;">

然后,在处理表单时,您采用$_POST['response']中接收的值,在管道中将其爆炸,您将获得是/否答案和学生证:

$verdict = explode("|", $_POST['response']);
$yay_or_nay = $verdict[0];
$whom = $verdict[1];

然后,您可以使用$yay_or_nay$whom做任何事情

答案 1 :(得分:0)

  

问题在于所有输入的名称都相同。

为它们提供唯一的值,并使用<button>元素,以使显示标签与该值不同。

<button type='submit' value='No-UNIQUE-ID' class='btn-link' style='border:none;'>
    No
</button>

答案 2 :(得分:-1)

我实际上有一个类似的问题。我解决问题的方法是在每行上有两个单选按钮,并在值中存储“是”或“否”部分。它们由分隔符与其他值(您的StudentID)分隔。然后,表格下方的“提交”按钮使用户可以批量提交响应。

您的代码应如下所示:

                <form method="post" name="data">
                <table style="overflow-x: auto; display: block; white-space: nowrap; max-height: 450px; max-width: 1100px;"
                       class="table table-striped">
                    <tr>
                        <th>ID</th>

                        <th>Student Name</th>
                        <th>Address</th>
                        <th>Contact No.</th>
                        <th>Email</th>
                        <th colspan="4">Accept / Reject</th>
                    </tr>
                    <?php
                    $query = "SELECT StudentID,StudentName,Address,ContactNo,Email FROM Student WHERE Accepted=0";
                    $mysqli = connect();
                    //Get result
                    $result = $mysqli->query($query) or die($mysqli->error . " " . __LINE__); //__LINE__ shows the line no. we are getting the error at

                    $count = $result->num_rows;
                    if ($result->num_rows > 0) {
                        //Loop through results
                        while ($row = mysqli_fetch_array($result)) {
                            //Display customer info
                            $output = "<tr>";
                            $output .= "<td>" . $row['StudentID'] . "</td>";

                            $output .= "<td>" . $row['StudentName'] . "</td>";
                            $output .= "<td >" . $row['Address'] . "</td>";
                            $output .= "<td >" . $row['ContactNo'] . "</td>";
                            $output .= "<td >" . $row['Email'] . "</td>";

                            $output .= "<td><input type='radio'  name='radio" . $count . "' value='" . $row['StudentID'] . "|accept' ></td>";

                            $output .= "<td><input type='radio'  name='radio" . $count . "' value='" . $row['StudentID'] . "|reject' ></td>";

                            $output .= "</tr>";
                            echo $output;
                            $count += 1;
                        }
                        echo "</table>";
                        echo "<input type='submit' name='submit' value='submit'>";

                    } else
                        echo "No pending request found";

                    ?>
                </table>
            </form>

然后您从选择的任何btn中获取值,并执行其余代码:

if(isset($_POST['submit'])) {
    for($x = 0; $x < $count; $x++) {
        if(isset($_POST['radio' . $x])) {
            $values = explode('|', $_POST['radio' . $x]);
            $id = $values[0];
            $status = $values[1];

            //Do stuff with each result
        }
    }
}

答案 3 :(得分:-1)

我遇到了类似的问题,不得不使用JS / jQuery

$(".myClass").bind("click", function() {
    var id = event.target.id;
    window.location.href = "myPage?id="+id;
});