Advertisement
Guest User

Untitled

a guest
Nov 29th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. <?php
  2. require('../connection/databaseConnection.php');
  3. $connect = db_connect();
  4. $role_query = "SELECT role_id, role_name FROM role";
  5. $role = mysqli_query($connect, $role_query)->fetch_all(MYSQLI_ASSOC);
  6.  
  7. $user_query = "SELECT user_id, username, password, role_name, active FROM user INNER JOIN role ON user.role_id = role.role_id";
  8. $user = mysqli_query($connect, $user_query)->fetch_all(MYSQLI_ASSOC);
  9. ?>
  10.  
  11. <!DOCTYPE html>
  12. <html>
  13. <head>
  14. <title>Cental Control | Create a user</title>
  15. <script type="text/javascript" src="../js/jquery-3.2.1.js"></script>
  16. </head>
  17. <body>
  18. <form action="../user/create.php" method="POST">
  19. <label>Username: </label>
  20. <input type="text" required name="username">
  21. <input type="password" required name="password">
  22. <select name="role">
  23. <?php
  24.  
  25. foreach ($role as $value) {
  26. $id = $value['role_id'];
  27. $role_name = $value['role_name'];
  28. echo "<option value='$id'>$role_name</option>";
  29. }
  30. ?>
  31. </select>
  32. <button type="submit">Submit</button>
  33. </form>
  34. <br><br><br>
  35. UPDATE<form action="../user/edit.php" method="POST">
  36.  
  37.  
  38. <input type="text" name="user_id">
  39. <input type="text" required name="username">
  40. <input type="password" required name="password">
  41. <select name="role">
  42. <?php
  43.  
  44. foreach ($role as $value) {
  45. $id = $value['role_id'];
  46. $role_name = $value['role_name'];
  47. echo "<option value='$id'>$role_name</option>";
  48. }
  49. ?>
  50. </select>
  51. <input type="text" name="active">
  52. <button type="submit">Submit</button>
  53. </form>
  54.  
  55. <br><br><br>
  56. DELETE<form action="../user/delete.php" method="POST">
  57.  
  58.  
  59. <input type="text" name="user_id">
  60. <button type="submit">Submit</button>
  61. </form>
  62. <br><br><br>
  63. -----------------
  64. <table style="border: 1px solid gray">
  65. <thead>
  66. <tr>
  67. <th></th>
  68. <th>USER ID</th>
  69. <th>USERNAME</th>
  70. <th>PASSWORD</th>
  71. <th>ROLE NAME</th>
  72. <th>ACTIVE</th>
  73. </tr>
  74. </thead>
  75. <tbody>
  76. <?php
  77. foreach ($user as $users) {
  78. $user_id = $users['user_id'];
  79. $username = $users['username'];
  80. $password = $users['password'];
  81. $rolename = $users['role_name'];
  82. $active = $users['active'];
  83.  
  84.  
  85. echo "<tr>";
  86. echo "<form name='editform' id='editform'>";
  87. echo "<td><input type='checkbox' name='check' data-id='$user_id' value='$user_id'></td>";
  88. echo "<td><span>$user_id</span><input id='uid' type='hidden' disabled value='$user_id'></td>";
  89. echo "<td><span>$username</span><input name='username' id='uuname' type='hidden' value='$username'></td>";
  90. echo "<td><span>$password</span><input name='password' id='upass' type='hidden' value='$password'></td>";
  91. echo "<td><span>$rolename</span><select id='rolename' name='rolename' style='display: none;'>";
  92. foreach ($role as $value) {
  93. $id = $value['role_id'];
  94. $role_name = $value['role_name'];
  95. echo "<option value='$id'>$role_name</option>";
  96. }
  97. echo "</select></td>";
  98. echo "<td><span>$active</span><input name='active' id='uactive' type='hidden'></td>";
  99. echo "<td><button id='editBtn' name='editBtn' type='button' data-id='$user_id' class='edit-btn' >EDIT</button><button id='confirmBtn' name='confirmBtn' hidden type='button' data-id='$user_id' class='confirm-btn' >CONFIRM</button></td>";
  100. echo "<td><button id='deleteBtn' name='deleteBtn' type='button' data-id='$user_id' class='delete-btn'>DELETE</button></td>";
  101. echo "</form>";
  102. } echo "</tr>";
  103. ?>
  104. </tbody>
  105. </table>
  106.  
  107. <script type="text/javascript">
  108. $(document).ready(function(){
  109. //Primary Button Confirm
  110. $('.edit-btn').on('click', function(){
  111.  
  112. var row = $(this).parent().parent();
  113. var toTrim = $(row).find('#editBtn').html();
  114. var trimmed = $.trim(toTrim);
  115. //console.log(trimmed);
  116.  
  117. var user_id = $('#uid').val();
  118. var username = $('#uuname').val();
  119. var password = $('#upass').val();
  120. var rolename = $('#rolename').val();
  121. var active = $('#uactive').val();
  122.  
  123.  
  124. if(trimmed == 'EDIT') {
  125. $(row).find('span').css('display', 'none');
  126. $(row).find('input[type="hidden"]').attr('type', 'text');
  127. $(row).find('select').show();
  128. $('table button').attr('disabled', 'disabled');
  129. $(row).find('#editBtn').html('CONFIRM').removeAttr('disabled').css('color', 'green');
  130. $(row).find('#deleteBtn').html('CANCEL').removeAttr('disabled');
  131. }
  132. else {
  133. $.ajax ({
  134. type: "POST",
  135. url: "edit.php",
  136. data: {user_id: user_id, username: username, password: password, rolename: rolename, active: active },
  137. success: function(result) {
  138. var result = JSON.parse(result);
  139. if(result.status == 'success') {
  140. alert(result.message);
  141. }
  142. }
  143. });
  144. }
  145. });
  146.  
  147. $('.confirm-btn').on('click', function(){
  148. alert('deleted');
  149. });
  150.  
  151.  
  152.  
  153.  
  154.  
  155. //Secondary Button Cancel Action
  156. $('.delete-btn').on('click', function(){
  157. var row = $(this).parent().parent();
  158. var toTrim = $(row).find('#deleteBtn').html();
  159. var trimmed = $.trim(toTrim);
  160. console.log(trimmed);
  161. if(trimmed == 'CANCEL') {
  162. var row = $(this).parent().parent();
  163. $(row).find('span').show();
  164. $(row).find('input[type="text"]').attr('type', 'hidden');
  165. $(row).find('select').css('display', 'none');
  166. $('table button').removeAttr('disabled');
  167. $(row).find('#confirmBtn').hide();
  168. $(row).find('#editBtn').show().html('EDIT').removeAttr('disabled');
  169. $(row).find('#deleteBtn').html($.trim('DELETE')).removeAttr('disabled');
  170. }
  171. else {
  172. $('table button').attr('disabled', 'disabled');
  173. $(row).find('#editBtn').hide();
  174. $(row).find('#confirmBtn').show().removeAttr('disabled');
  175. $(row).find('#deleteBtn').html('CANCEL').removeAttr('disabled');
  176. }
  177.  
  178.  
  179.  
  180. });
  181.  
  182.  
  183. });
  184. </script>
  185. </body>
  186. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement