[英]Pass Two values when a Submit button is clicked -PHP
This table is displaying details about my workload table per row. 该表每行显示有关我的工作量表的详细信息。 And when I click my "pre-View_attend" submit button I want to get the "WorkloadID" and "subjectID" of that workload row where the submit button was clicked.
当我单击“ pre-View_attend”提交按钮时,我想获得单击了提交按钮的工作负载行的“ WorkloadID”和“ subjectID”。
Currently I have a hidden field containing the subject ID which is "subjectID_hidden", and when I try to get the "WorkloadID" and "subjectID" by using isset. 当前,我有一个包含主题ID的隐藏字段,该主题ID为“ subjectID_hidden”,当我尝试使用isset来获取“ WorkloadID”和“ subjectID”时。
It seems that I can't accurately get the exact subject ID of the row where I've clicked the "pre-View_attend" submit button. 似乎我无法准确获得单击“ pre-View_attend”提交按钮的行的确切主题ID。
<tbody style="font-size:20px; text-align:center;"> <tr> <td><?php echo $row["subjectName"]; ?></td> <td><?php echo $row["className"]; ?></td> <td><?php echo $row["RoomNumber"]; ?></td> <td><?php echo $Sched_Days.'<br>'.$FST.' To '.$FET;?></td> <td><button style="font-size:15px;" type="button" id="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary view_StudentList">View Student List</button></td> <td><button style="font-size:15px;" type="submit" name="gettingAttendance" value="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary">Take Attendance</button></td> <td><button style="font-size:15px;" type="submit" name="pre-View_attend" value="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary">View Attendance</button></td> <input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>"> </tr> <?php } mysqli_close($connect);?> </tbody> /* I'm getting the workload ID accurately, but the subject ID is incorrect. I believe I'm getting the last subject ID that my query produced */ <?php if(isset($_POST['pre-View_attend'])) { $FWID=$_POST['pre-View_attend']; $FSJID=$_POST['subjectID_hidden']; echo"Workload ID: $FWID SubjectID: $FSJID"; } ?>
You get the last value because you have a bunch of hidden fields with the same name. 获得最后一个值是因为您有一堆同名的隐藏字段。
You could name them subjectID_hidden[<?=$row['WorkloadID'];?>]
to get an array to then get the subjectID
by WorkloadID
: 您可以将它们命名为
subjectID_hidden[<?=$row['WorkloadID'];?>]
以获取一个数组,然后通过WorkloadID
获得subjectID
:
$subjectID = $_POST["subjectID_hidden"][$_POST["pre-View_attend"]];
You need to query the table for something unique to the row you want to retrieve information from. 您需要在表中查询要从中检索信息的行所特有的内容。 This unqiue data should already be existing on the page, encoded into the button/form.
页面上应该已经存在这种不正确的数据,已将其编码为按钮/表单。 Example;
例;
<?php
if(isset($_POST['pre-View_attend'])) {
global $db;
$select = "select * from mytable where unique='$unique'";
$connect = mysqli_query($db, $select);
while($row=mysqli_fetch_array($connect)) {
$myfirstVariable = $row['myfirstVariable'];
$mysecondVariable = $row['mysecondVariable'];
$show = "$myfirstVariable $mysecondVariable";
}
}
?>
Then you can call $show when you want in the HTML. 然后,您可以根据需要在HTML中调用$ show。 ps there are quite a few errors in your html like;
PS在你的HTML像有很多错误;
<input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>">
should be 应该
<input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>" />
self closing at the end with forwards slash. 在结尾处以正斜杠自我关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.