[英]How can I pass values other than form input values through POST method
我在admin.php文件的表單中有多個提交按鈕。 對於管理員執行的每個操作,我在admin.php文件中都有一個提交按鈕。 管理員的一項工作(每日出勤)是將員工的出勤標記為存在或不存在。
當我單擊“每日出勤”的提交按鈕時,會出現3個下拉選項,允許管理員選擇年,月和日來標記出勤。 隨之顯示一個表,其中包含employee表中的所有記錄。 對於顯示的每個記錄,我正在動態生成一個用於出席的單選按鈕(值:存在和不存在)和一個提交按鈕(名稱=標記)。 管理員只需借助單選按鈕即可將任何一名員工標記為在職/不在職,然后單擊相鄰的提交按鈕(標記)以將此記錄插入到出勤表中。
每個記錄的選擇(年,月,日)和單選按鈕是動態生成的表單的一部分。 在這種形式中,我將ACTION屬性指定為“ mark-attendance.php”,方法為POST。
現在,我想傳遞選定的年,月,日值; 通過POST方法將當前或缺勤的出勤值以及記錄了該出勤記錄的員工的員工ID轉到“ mark-attendance.php”頁面。
我能夠檢索年,月,日(選擇)和出勤值(當前/缺席)的值。 但是我無法將該人的員工ID的值傳遞給“ mark-attendance.php”,因為它不是表單輸入。
如何傳遞此值? 我想使用此值在該特定員工id的出勤表中插入一條記錄。
任何幫助將不勝感激 :
這是我的代碼:由於空間限制,我已經編輯了我的代碼
switch ($_POST['admin']) {
// if admin=>Daily Attendance
case 'Daily Attendance':
$sql_sel_emp = "select * from employee";
$res_emp = mysqli_query($conn,$sql_sel_emp);
$aff_sel = mysqli_affected_rows($conn);
//display a calendar and table with records only if records exist
if ($aff_sel>0) {
echo "<form action='test-message.php' method='POST'>
<table>
<br><br>
<tr>
<td>
<select name='year'>
<option value='2016'>2016</option>
<option value='2017'>2017</option>
</select>
</td>
<td>
<select name='month'>
<option value='01'>January</option>
<option value='02'>February</option>
</select>
</td>
<td>
<select name='day'>
<option value='01'>1</option>
<option value='02'>2</option>
</select>
</td>
</tr>
</table><br><br>";
echo "<table border='1' cellpadding='1' cellspacing='1'>
<tr>
<th>Employee Image</th>
<th>Employee Id</th>
<th>Employee Name</th>
<th>Employee Email</th>
<th>Employee DOB</th>
<th>Employee Designation</th>
<th>Employee Department</th>
<th>Attendance Status</th>
<th>Action</th>
</tr>";
while ($row=mysqli_fetch_assoc($res_emp)) {
echo "<tr>
<td>";?><img src="images/<?php echo $row['emp_image'];
?>" height="100" width="100" ><?php echo "</td>
<td>".$row['emp_id']."</td>
<td>".$row['emp_name']."</td>
<td>".$row['emp_email']."</td>
<td>".$row['emp_dob']."</td>
<td>".$row['emp_designation']."</td>
<td>".$row['emp_department']."</td>
<td><input type='radio' name='attendance'
value='present'>Present<br>
<input type='radio' name='attendance' value='absent'>Absent<br></td>
<td>
<input type='submit' name='mark' value='Mark Attendance'>
</td>
</tr>";
}
echo "</table></form>";
//display message if there are no records in temporary_employee table
} else {
$msg="No records found";
}
break;
首先,在回顯html代碼時,請使用“而不是”。
echo '<a href="#">'
更容易編寫。
因此,對於您的問題,我將通過以下方式進行:
添加一個額外的隱藏輸入,將員工ID作為其值,並在php中進行檢查。 有關更多信息,請參見此線程: 通過php中的POST輸入具有相同名稱的多個輸入
..
<td>' . $row['emp_id'] . '<input type="hidden" name="employee[]" value="' . $row['emp_id'] . '"</td>
..
您不必將name
屬性放在數組中,因為您正在while
循環內運行form
。 您只需將其添加到form
任何位置
<input type="hidden" name="emp_id" value="<?php echo $row['emp_id']; ?>" />
這樣,您可以通過$_POST['emp_id'];
在另一頁中檢索員工的ID $_POST['emp_id'];
我已經為顯示的每個員工記錄動態生成了一個表格。 生成的每個表單都包含以下內容
這樣,我便可以將值傳遞到標記每個員工出勤的Attenance.php文件。
以下代碼段運行良好 。 我已經編輯了代碼,以提高可讀性並解決空間受限的問題。
admin.php
case 'Mark Attendance':
$sql_sel_emp = "select * from employee";
$res_emp = mysqli_query($conn, $sql_sel_emp);
$aff_sel = mysqli_affected_rows($conn);
//display table only if records exist
if ($aff_sel>0) {
echo "<br>
<table border='1' cellpadding='1' cellspacing='1' width='1500'>
<tr>
<th>Employee Image</th>
<th>Employee Id</th>
<th>Employee Name</th>
<th>Employee Email</th>
<th>Employee DOB</th>
<th>Employee Designation</th>
<th>Employee Department</th>
<th>Attendance Date</th>
<th>Attendance Status</th>
<th>Action</th>
</tr>" ;
while ($row = mysqli_fetch_assoc($res_emp)) {
echo "<tr><form action='attendance.php' method='POST'>
<td>";?><img src="images/<?php echo $row['emp_image'];
?>" height="100" width="100" ><?php echo "</td>
<td>".$row['emp_id']."<input type='hidden' name='emp_id' value='".$row['emp_id']."'></td>
<td>".$row['emp_name']."<input type='hidden' name='emp_name' value='".$row['emp_name']."'></td>
<td>".$row['emp_email']."</td>
<td>".$row['emp_dob']."</td>
<td>".$row['emp_designation']."</td>
<td>".$row['emp_department']."</td>
<td><select name='year'>
<option value='2016'>2016</option>
<option value='2017'>2017</option>
</select>
<select name='month'>
<option value='01'>January</option>
<option value='02'>February</option>
</select>
<select name='day'>
<option value='01'>1</option>
<option value='02'>2</option>
</select>
</td>
<td><input type='radio' name='attendance' value='present' checked>Present<br>
<input type='radio' name='attendance' value='absent'>Absent<br></td>
</td>
<td>
<input type='submit' name='mark' value='Mark Attendance'>
</td>
</form>
</tr>
";
}
echo "</table>";
//display message if there are no records in temporary_employee table
} else {
echo "No records found";
}
break;
}
Attenance.php
$conn = mysqli_connect('localhost','root','','attendance');
if (isset($_POST['mark'])) {
//capture $_POST values
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$date = $year.$month.$day;
$empid = $_POST['emp_id'];
$attend = $_POST['attendance'];
$empname = $_POST['emp_name'];
//check if the attendance is already marked for the employee on that day
$sql_sel = "select * from `attendance` where emp_id='$empid' and date='$date';";
$res_sel = mysqli_query($conn, $sql_sel);
$aff_sel = mysqli_affected_rows($conn);
//If the attendance is already marked for the employee on that day ,
//send a message back to admin
if ($aff_sel==1) {
$_SESSION['message'] = "The attendance of $empname is already
marked for $day $month $year";
Header("Location: admin.php");
//check if there are mutiple entries for attendance for the employee on that day
//send a message back to admin
} else if ($aff_sel>1) {
$_SESSION['message'] = "There are multiple attendance entries
for $empname for $day $month $year";
Header("Location: admin.php");
//go ahead and insert if the attendance for the employee is not marked for the day
} else if ($aff_sel==0) {
$sql_ins = "INSERT INTO `attendance`(emp_id,date,status) VALUES('$empid','$date','$attend');";
mysqli_query($conn, $sql_ins);
//check if the record is inserted
$aff_ins = mysqli_affected_rows($conn);
//send a success message back to admin if the record is inserted
if ($aff_ins==1) {
$_SESSION['message'] = "$empname has been marked $attend for
$day $month $year";
Header("Location: admin.php");
//send a failure message back to admin if the record was not inserted
} else if ($aff_ins==0) {
$_SESSION['message'] = "The attendance of $empname was not recorded for the day";
Header("Location: admin.php");
//send a failure message back to admin if the insert query failed
} else if ($aff_ins<0) {
$_SESSION['message'] = "There was an error ...Try again";
Header("Location: admin.php");
}
//return an error message to the admin if there was an error while firing the query
} else if ($aff_sel<0) {
$_SESSION['message'] = "There was an error ..Try again";
Header("Location: admin.php");
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.