如何通过POST方法传递表单输入值以外的值

时间:2016-08-25 08:39:48

标签: php mysql

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

当我点击"每日出勤"的提交按钮时,出现3个下拉选项,允许管理员选择年份,月份和日期来标记出勤率。除此之外,还会显示一个表,其中包含employee表中的所有记录。对于显示的每条记录,我动态生成一个用于出勤的单选按钮(值:存在和不存在)和一个提交按钮(名称=标记)。管理员只需在单选按钮的帮助下将任何一名员工标记为现在/不在,并点击相邻的提交按钮(标记)将该记录插入考勤表。

每条记录的选择(年,月,日)和单选按钮是动态生成表单的一部分。在这种形式中,我将ACTION属性赋予" mark-attendance.php"和method = POST。

现在,我想传递年,月和日期的选定值;出现或缺席的出勤值,以及记录出勤记录的员工的员工ID,到页面" mark-attendance.php"通过POST方法。

我能够检索年,月,日(选择)和出勤值(出现/缺席)的值。但我无法将该人员的员工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;

3 个答案:

答案 0 :(得分:0)

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

所以对于你的问题,我会按照以下方式进行:

添加一个额外的隐藏输入,其中包含员工ID作为其值     在php中查看它。有关详细信息,请参阅此主题:Multiple inputs with same name through POST in php

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

答案 1 :(得分:0)

由于您在name循环内运行form,因此无需将while属性放入数组中。您只需在form

中的任意位置添加此内容即可
<input type="hidden" name="emp_id" value="<?php echo $row['emp_id']; ?>" />

这样您就可以通过$_POST['emp_id'];

检索其他页面中的员工ID

答案 2 :(得分:0)

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

  • 每位员工的详细信息(例如图片,员工ID,员工姓名等)
  • 隐藏类型输入字段,用于存储员工的员工ID
  • 隐藏类型输入字段,用于存储员工的员工姓名
  • 年份,月份和日期的日历选择
  • 单选按钮,用于将出席情况标记为存在或不存在
  • 提交按钮

这样,我就可以将值传递给attendance.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;

    }

attendance.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");

    }

}

?>