douyabu1528 2016-08-25 08:39
浏览 57
已采纳

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

I have multiple submit buttons in a form in admin.php file. For each action that the admin does, I have a submit button in admin.php file. One of the actions of an admin (Daily Attendance), is to mark the attendance of employees as present or absent.

When I click on the submit button for "Daily Attendance" , 3 dropdown selections appears which allows the admin to choose the year,month and date to mark the attendance. Along with this, a table is displayed with all the records from the employee table . For each record that is displayed , I am dynamically generating a radio button for attendance (values : present and absent) and a submit button (name = mark) . The admin simply marks any one of the employees as present/absent with the help of radio button , and clicks on the adjoining submit button(mark) to insert this record in the attendance table.

The selections (year,month , date ) and the radio buttons for each record are a part of a dynamically generated form . In this form I have given the ACTION attribute as "mark-attendance.php" and method = POST.

Now , I want to pass the selected value for year,month and date ; the attendance value for present or absent ,and the employee id for the employee whose record the attendance is being marked, to the page "mark-attendance.php" through POST method .

I am able to retrieve the values for year,month,date (selection) and attendance value(present/absent) . But I am unable to pass the value of employee id of the person to "mark-attendance.php" , since it is not a form input .

How do pass this value ? I want to use this value to insert a record in attendance table for that particular employee id .

Any help will be appreciated :

Here is my code : I have edited my code for space constraints

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条回答 默认 最新

  • douyin8809 2016-08-31 14:53
    关注

    I have dynamically generated a form for each employee record that is displayed . Each form that is generated contains the following

    • Details of every employee(such as image, employee id, employee name, etc)
    • a hidden type input field that stores the employee id of the employee
    • a hidden type input field that stores the employee name of the employee
    • a calendar selection each for year, month, and date
    • radio buttons to mark the attendance as present or absent
    • a submit button

    This way , I am able to pass the values to attendance.php file which marks the attendance of each employee .

    The following piece of code works perfectly well . I have edited my code to promote readability as well as to address the problem of space constraint .

    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");
    
        }
    
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?