[英]Two php forms. second form gets data from first. but refreshing second page shows error
我正在顯示數據庫中的設備信息列表。 每個設備都有一個“顯示警告”按鈕。 用戶可以點擊“顯示警告”,我使用隱藏字段(隱藏字段中的值來識別)來顯示設備警告消息。 數據庫中的每個警告都有一個接受按鈕(每個警告列表都有接受按鈕)。 用戶可以單擊“接受”按鈕,以便警告的狀態將從未接受變為已接受(並且頁面將刷新)。 問題是刷新,因為它沒有第一頁的信息,PHP返回錯誤。
//page-1.. under each category (like electrical, mechanical, more than one types of devices
//device can be identified by using ID
echo '<tr><th>Category Name</th>';
echo '<td >' . $row['category']. '</td></tr>';
echo '<tr><th>Device ID</th>';
echo '<td >' . $row['ID']. '</td></tr>';
//form section.
echo "<form action = alarms_list_display.php method =post>";
echo '<tr>';
echo "<td >"." <input type=submit name=acceptID value=ShowAlarm"." </td>";
echo "<td >"." <input type=hidden name=hiddenID value =". $row["ID"]." </td>";
echo "</tr>";
echo "</form>";
//second page.. alarms_list_display.php
include ("DBconnect.php");
$conn= mysqli_connect( $dbhost, $dbuser, $dbpass, $db );
if(isset($_POST ['acceptID']))
{$deviceID = $_POST['hiddenID'];
}
else {$deviceID='';}
//above code is problematic. as it is available only when i submit the first page.
//when updating the second page (and refreshing), $deviceID is not available anymore.
//listing all warnings related to that device when user clicked the 'ShowAlarm' button.
if ($conn->connect_error) {die("Connection failed: " . $conn->connect_error);}
$sql = "SELECT * FROM dataTable WHERE ID='$deviceID' ";
$result = $conn->query($sql);
//form with buttons to accept warning
//skipped table heading parts
//each warning will have a serialNumber. Using to accept the warning and update the database
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<form action = alarms_list_display_copy.php method =get>";
echo "<td>" . $row["Date"]. "</td>";
echo "<td>" . $row["Warning"]. "</td>";
echo "<td>"." <input type=hidden name=hidden value =" .$row["SerialNumber"]." </td>";
echo "<td>"." <input type=submit name=accept value=AcceptAlarm id=button1 class = formclass"." </td>";
echo "</tr>";
echo "</form>";
} else { echo "0 results"; }
//code to update when clicking the AcceptAlarm button.
if (isset($_GET['accept'])){
$query= "SELECT WarningAccept FROM dataTable WHERE SerialNumber= '$_GET[hidden]' ";
$result = mysqli_query($conn, $query);
while($row = $result->fetch_assoc()) {
$updateQuery = " UPDATE dataTable SET WarningAccept=1 WHERE SerialNumber= '$_GET[hidden]' ";
mysqli_query($conn, $updateQuery);
}
可以提交第一頁。 它將顯示與設備對應的所有警告。 但是當接受警告時,頁面將刷新並且$ deviceID不再存在。 所以更新后它不會顯示任何警告信息。 怎么改呢??? 任何幫助
你的second page
只有一次<form action = alarms_list_display.php method =post>";
得到提交,這就是你在刷新第二頁時發生錯誤的原因,因為它丟失了$_POST
數組然后
解
您可以將<form action = alarms_list_display.php method =post>"
設置為同一頁面並在會話中存儲值,然后將其重定向到第二頁,然后您可以使用會話中的值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.