0

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;
Sabyasachi Gupta
  • 153
  • 2
  • 13
  • 1
    Why don't you create an `` and set its value to whatever you're trying to send ? – roberto06 Aug 25 '16 at 08:42
  • @roberto06 , I am able to pass the value of employee id through a hidden type input field . But the problem I am facing is that no matter whichever employee i am clicking on , I am getting the employee id of only the last employee, in the other page ,through POST method . I have simply added the code below to my existing code where i am fetching all the records from the employee table .
    – Sabyasachi Gupta Aug 25 '16 at 09:06
  • You should have a `
    ` element per user, the problem here is that there are multipple hidden fields with the same name, in only one form, so it will take the last value.
    – roberto06 Aug 25 '16 at 09:10
  • @roberto06..i could use a form element for each employee . But I am unable to understand how will i pass the value of yyyy,mm,dd selections which would be inputs from another form . This date selection is used only once by the admin. Any help will be appreciated . – Sabyasachi Gupta Aug 25 '16 at 14:18
  • Two solutions come to mind: 1. If you use multiple `
    ` elements, create `` elements in each `
    ` with the values from your dropdown (which can be updated on change with a simple JS). 2. With only one `
    ` element, you could transform your `` elements into ``, this way `$_POST['mark']` will be set to the ID you wished to pass.
    – roberto06 Aug 25 '16 at 14:54
  • @roberto06..i have used a form element for each employee record along with a hidden type input field to store the employee id of ecah employee . It works fine . As for the calendar input , i was asked by my project head to give a calendar selection in the form itself which is generated for each employee and this works well too.(instead of giving a calendar selection outside the form ).thks for your help – Sabyasachi Gupta Aug 26 '16 at 10:19

3 Answers3

0

First of all, use ' instead of " when echoing an html code.
echo '<a href="#">' is easier to write.

So to your question, i would do it the following way:

Add an extra hidden input with employee id as its value and check it in php. See this thread for more info : Multiple inputs with same name through POST in php

    ..
    <td>' . $row['emp_id'] . '<input type="hidden" name="employee[]" value="' . $row['emp_id'] . '"</td>
    ..
Community
  • 1
  • 1
Mightee
  • 689
  • 7
  • 22
  • by this method , I am able to retrieve all the values of the hidden field which is stored in the array $_POST['employee'] . But , i am unable to understand , how would I know the value of employee id for which I have clicked the submit button (for each employee there is a corresponding submit button ) . I wanted to use this emp_id in the other page to insert the attendance record in the attendance table for that particular emp id . – Sabyasachi Gupta Aug 25 '16 at 14:13
  • then i would suggest you to put a form for every employee and change employee[] to employee_id and you are good to go. – Mightee Aug 26 '16 at 07:05
0

You don't have to put the name attribute in an array since you are running your form inside the while loop. You just add this anywhere within your form

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

This way you can retrieve the employee's id in the other page by $_POST['emp_id'];

KANAYO AUGUSTIN UG
  • 2,078
  • 3
  • 17
  • 31
0

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

    }

}

?>
Sabyasachi Gupta
  • 153
  • 2
  • 13