【问题标题】:How can I pass values other than form input values through POST method如何通过 POST 方法传递表单输入值以外的值
【发布时间】:2016-08-25 08:39:47
【问题描述】:

我在 admin.php 文件的表单中有多个提交按钮。对于管理员执行的每个操作,我在 admin.php 文件中都有一个提交按钮。管理员的一项操作(每日考勤)是将员工的考勤标记为出席或缺席。

当我点击“每日考勤”的提交按钮时,会出现 3 个下拉选项,让管理员可以选择年、月和日期来标记考勤。除此之外,还会显示一个表,其中包含员工表中的所有记录。对于显示的每条记录,我正在动态生成一个出勤单选按钮(值:出席和缺席)和一个提交按钮(名称 = 标记)。管理员只需使用单选按钮将任何一名员工标记为存在/缺席,然后单击相邻的提交按钮(标记)以将此记录插入到考勤表中。

每条记录的选择(年、月、日)和单选按钮是动态生成表单的一部分。在这种形式中,我将 ACTION 属性指定为“mark-attendance.php”和 method = 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;

【问题讨论】:

  • 您为什么不创建一个&lt;input type="hidden" /&gt; 并将其值设置为您要发送的任何内容?
  • @roberto06 ,我能够通过隐藏类型输入字段传递员工 ID 的值。但我面临的问题是,无论我点击哪个员工,我都只能通过 POST 方法在另一个页面中获取最后一个员工的员工 ID。我只是将下面的代码添加到我现有的代码中,我正在从员工表中获取所有记录。
    跨度>
  • 每个用户应该有一个&lt;form /&gt; 元素,这里的问题是有多个同名的隐藏字段,只有一种形式,所以它会取最后一个值。
  • @roberto06..i 可以为每个员工使用一个表单元素。但我无法理解我将如何传递 yyyy,mm,dd 选择的值,这将是来自另一种形式的输入。此日期选择仅由管理员使用一次。任何帮助将不胜感激。
  • 想到两个解决方案: 1. 如果您使用多个 &lt;form /&gt; 元素,请使用下拉列表中的值在每个 &lt;form /&gt; 中创建 &lt;input type="hidden" /&gt; 元素(可以使用简单的 JS)。 2.只有一个&lt;form /&gt;元素,你可以将你的&lt;input type='submit' name='mark' value='Mark Attendance'&gt;元素转换成&lt;button type="submit" name="mark" value="THE_ELEMENT_ID_YOU_WISH_TO_PASS"&gt;Mark Attendance&lt;/button&gt;,这样$_POST['mark']就会被设置为你想要传递的ID。

标签: php mysql


【解决方案1】:

首先,在回显 html 代码时使用 ' 而不是 "。
echo '&lt;a href="#"&gt;' 更容易编写。

所以对于你的问题,我会这样做:

添加一个额外的隐藏输入,以员工 ID 作为其值,并且 在 php.ini 中检查它。有关更多信息,请参阅此线程:Multiple inputs with same name through POST in php

    ..
    <td>' . $row['emp_id'] . '<input type="hidden" name="employee[]" value="' . $row['emp_id'] . '"</td>
    ..

【讨论】:

  • 通过这种方法,我能够检索存储在数组 $_POST['employee'] 中的隐藏字段的所有值。但是,我无法理解,我怎么知道我点击了提交按钮的员工 ID 的值(每个员工都有一个相应的提交按钮)。我想在其他页面中使用这个 emp_id 将出勤记录插入到该特定 emp id 的出勤表中。
  • 那么我建议您为每个员工填写一个表格并将employee[] 更改为employee_id,然后您就可以开始了。
【解决方案2】:

您不必将name 属性放入数组中,因为您在while 循环中运行form。您只需在 form 中的任何位置添加它即可

<input type="hidden" name="emp_id" value="<?php echo $row['emp_id']; ?>" />

这样你就可以在另一页通过$_POST['emp_id'];检索员工的id

【讨论】:

    【解决方案3】:

    我已经为每个显示的员工记录动态生成了一个表单。生成的每个表单都包含以下内容

    • 每位员工的详细信息(如图片、员工 ID、员工姓名等)
    • 一个隐藏类型的输入字段,存储员工的员工ID
    • 一个隐藏的类型输入字段,用于存储员工的员工姓名
    • 年、月、日的日历选择
    • 单选按钮来标记出席或缺席
    • 提交按钮

    这样,我可以将值传递给标记每个员工的出勤的出席.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;
    
        }
    

    出席.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");
    
        }
    
    }
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      相关资源
      最近更新 更多