2016-08-25 19 views
0

admin.phpファイルのフォームに複数の送信ボタンがあります。管理者が行う各アクションについては、admin.phpファイルに送信ボタンがあります。管理者の行動の1つ(毎日の出席)は、従業員の出席を存在または不在としてマークすることです。POSTメソッドでフォーム入力値以外の値を渡す方法

"Daily Attendance"の送信ボタンをクリックすると、3つのドロップダウン選択が表示され、管理者は出席をマークする年、月、日を選択できます。これに伴い、従業員テーブルのすべてのレコードを含むテーブルが表示されます。表示されるレコードごとに、出席用のラジオボタン(値:presentとabsent)と送信ボタン(name = mark)を動的に生成しています。管理者は、従業員の一人にラジオボタンの助けを借りて存在/不在のマークを付けるだけで、隣接する送信ボタン(マーク)をクリックしてこのレコードを出席テーブルに挿入します。

各レコードの選択(年、月、日付)とラジオボタンは、動的に生成されるフォームの一部です。このフォームでは、ACTION属性に "mark-attendance.php"とmethod = POSTを指定しました。

ここで、選択した値を年、月、および日に渡します。出席がマークされている従業員の現在または不在の出席値、および従業員IDをPOSTメソッドを使用して「mark-attendance.php」ページに追加します。

私は年、月、日付(選択)と出席者(現在/不在)の値を取得できます。しかし、それはフォームの入力ではないので、その人の従業員IDの値を "mark-attendance.php"に渡すことができません。

どのようにこの値を渡すのですか?この値を使用して、特定の従業員IDの出席テーブルにレコードを挿入します。

すべてのヘルプは理解されるであろう。ここでは

が私のコードです:HTMLコードをエコーするとき、私は「スペースの制約すべての

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; 
+1

は、なぜあなたがしているものは何でも、その値を 'の'を作成して、設定しないでください送信しようとしている? – roberto06

+0

@ roberto06、私は社員IDの値を隠し型入力フィールドに渡すことができます。しかし、私が直面している問題は、どちらの従業員がクリックされても、POSTメソッドを通じて、他のページの最後の従業員だけの従業員IDを取得していることです。 以下のコードを私の既存のコードに追加しました。ここで私はemployeeテーブルからすべてのレコードを取得しています。 ​​の

+0

あなたはユーザーあたり'

'要素を持っている必要があり、ここで問題となるのは、同じ名前の複数の隠しフィールドが1つのフォームしかないので、最後の値をとるということです。 – roberto06

答えて

0

することにより、他のページに従業員のIDを取得することができます。この方法は、このどこかを追加します。生成された各フォームは、(例えば、画像、従業員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"); 

    } 

} 

?> 
0

まず、使用 'の代わりのための私のコードを編集した
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> 
    .. 
+0

このメソッドでは、配列$ _POST ['employee']に格納されている隠しフィールドの値をすべて取得できます。しかし、私は理解できません。従業員IDの価値をどのように知っているのですか?私は従業員IDの値を知っていますか(従業員ごとに対応する送信ボタンがあります)。他のページでこのemp_idを使用して、その特定のemp idの出席テーブルに出席記録を挿入したかったのです。 –

+0

私はすべての従業員のためのフォームを配置し、従業員[]をemployee_idに変更することをお勧めします。 – Mightee

0

あなたはwhileループ内であなたのformを実行しているので、配列にname属性を配置する必要はありません。あなたはちょうど私が動的に表示されている各従業員レコードのフォームを生成しているあなたのform

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

以内にあなたが$_POST['emp_id'];

関連する問題