Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. // ----- Edit Row -----
  2.  
  3. $(document).on("click", "#html_master .edit", function () {
  4. var $this = $(this);
  5. var tds = $this.closest('tr').find('td').not('.mr_id').filter(function () {
  6. return $(this).find('.edit').length === 0;
  7. });
  8. if ($this.val() === 'Edit') {
  9. $this.val('Save');
  10. if($this.id != '.mr_id'){
  11. tds.prop('contenteditable', true);
  12. }
  13. } else {
  14. var isValid = true;
  15. var errors = '';
  16. $('#myDialogBox').empty();
  17. var elements = tds;
  18. if (tds.find('input').length > 0) {
  19. elements = tds.find('input');
  20. }
  21. var dict = {};
  22. elements.each(function (index, element) {
  23. var type = $(this).attr('class');
  24. var value = (element.tagName == 'INPUT') ? $(this).val() : $(this).text();
  25. console.log(type);
  26. // ----- Switch statement that provides validation for each table cell -----
  27. switch (type) {
  28. case "mr_id":
  29. dict["MR_ID"] = value;
  30. break;
  31. case "mr_name":
  32. if (value == value.match(/^[a-zA-Zs]+$/)) {
  33. dict["MR_Name"] = value;
  34. break;
  35. }
  36. case "buyer_id":
  37. if (!$.isNumeric(value)) {
  38. isValid = false;
  39. errors += "Please enter a valid Buyer IDn";
  40. }
  41. if (isValid) {
  42. dict["Buyer_ID"] = value;
  43. }
  44. break;
  45. case "poc_n":
  46. if (value == value.match(/^[a-zA-Zs]+$/)) {
  47. dict["MR_POC_N"] = value;
  48. break;
  49. }
  50. else {
  51. isValid = false;
  52. errors += "Please enter a valid Namen";
  53. }
  54. break;
  55. case "poc_e":
  56. if (value == value.match(/^[w-.+]+@[a-zA-Z0-9.-]+.[a-zA-z0-9]{2,4}$/)) {
  57. dict["MR_POC_E"] = value;
  58. break;
  59. }
  60. else {
  61. isValid = false;
  62. errors += "Please enter a valid Emailn";
  63. }
  64. break;
  65. case "poc_p":
  66. if (value == value.match('^[0-9 ()+/-]{10,}$')) {
  67. dict["MR_POC_P"] = value;
  68. break;
  69. }
  70. else {
  71. isValid = false;
  72. errors += "Please enter a valid Phone Numbern";
  73. }
  74. break;
  75. }
  76. })
  77. if (isValid) {
  78. console.log(dict);
  79. $this.val('Edit');
  80. tds.prop('contenteditable', false);
  81. var request = $.ajax({
  82. type: "POST",
  83. url: "update-copy.php",
  84. data: dict
  85. });
  86.  
  87. request.done(function (response, textStatus, jqXHR){
  88. if(JSON.parse(response) == true){
  89. console.log("row updated");
  90. } else {
  91. console.log("row failed to updated");
  92. console.log(response);
  93. console.log(textStatus);
  94. console.log(jqXHR);
  95. }
  96. });
  97.  
  98. // Callback handler that will be called on failure
  99. request.fail(function (jqXHR, textStatus, errorThrown){
  100. // Log the error to the console
  101. console.log(textStatus);
  102. console.log(jqXHR);
  103. console.error(
  104. "The following error occurred: "+
  105. textStatus, errorThrown
  106. );
  107. });
  108.  
  109. // Callback handler that will be called regardless
  110. // if the request failed or succeeded
  111. request.always(function () {
  112.  
  113. });
  114. } else {
  115. alert(errors);
  116. }
  117. }
  118. });
  119.  
  120. <?php
  121.  
  122. $MR_ID = $_POST['MR_ID'];
  123. $MR_Name = $_POST['MR_Name'];
  124. $Buyer_ID = $_POST['Buyer_ID'];
  125. $MR_POC_N = $_POST['MR_POC_N'];
  126. $MR_POC_E = $_POST['MR_POC_E'];
  127. $MR_POC_P = $_POST['MR_POC_P'];
  128.  
  129. $host="xxxxxxxxxx";
  130. $dbName="xxxxx";
  131. $dbUser="xxxxxxxxxxxx";
  132. $dbPass="xxxxxxxxx";
  133.  
  134. $pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);
  135.  
  136. $sql = "UPDATE Stage_Rebate_Master SET MR_Name = :MR_Name, Buyer_ID = :Buyer_ID, MR_POC_N = :MR_POC_N, MR_POC_E = :MR_POC_E, MR_POC_P = :MR_POC_P WHERE MR_ID = :MR_ID";
  137. $stmt = $pdo->prepare($sql);
  138. $stmt->bindValue(':MR_ID', $MR_ID);
  139. $stmt->bindValue(':MR_Name', $MR_Name);
  140. $stmt->bindValue(':Buyer_ID', $Buyer_ID);
  141. $stmt->bindValue(':MR_POC_N', $MR_POC_N);
  142. $stmt->bindValue(':MR_POC_E', $MR_POC_E);
  143. $stmt->bindValue(':MR_POC_P', $MR_POC_P);
  144. $result = $stmt->execute();
  145. echo json_encode($result);
  146.  
  147. ?>
  148.  
  149. <body>
  150.  
  151. <img src="image.png" id="image">
  152.  
  153. <form>
  154. Table Name: <input type="text" value="Stage_Rebate_Master" id="tableNameInput" readonly>
  155. <button class="create-user" id="insertButton">Add Vendor</button>
  156. </form>
  157.  
  158.  
  159. <div id="dialog-form" title="Add Vendor">
  160. <p class="validateTips">All form fields are required.</p>
  161.  
  162. <!-- Dialog box displayed after add row button is clicked -->
  163. <form>
  164. <fieldset>
  165. <label for="mr_name">Vendor</label>
  166. <input type="text" name="mr_name" id="mr_name" class="text ui-widget-content ui-corner-all" value="test">
  167. <label for="buyer_id">Buyer ID</label>
  168. <input type="text" name="buyer_id" id="buyer_id" class="text ui-widget-content ui-corner-all" value="99">
  169. <label for="poc_n">POC Name</label>
  170. <input type="text" name="poc_n" id="poc_n" class="text ui-widget-content ui-corner-all" value="name">
  171. <label for="poc_p">POC Email</label>
  172. <input type="text" name="poc_e" id="poc_e" class="text ui-widget-content ui-corner-all" value="test@tst.com">
  173. <label for="poc_p">POC Phone</label>
  174. <input type="text" name="poc_p" id="poc_p" class="text ui-widget-content ui-corner-all" value="5555555555">
  175.  
  176. <!-- Allow form submission with keyboard without duplicating the dialog button -->
  177. <input type="submit" id="submit" tabindex="-1" style="position:absolute; top:-1000px">
  178. </fieldset>
  179. </form>
  180. </div>
  181.  
  182.  
  183.  
  184. <div id="users-contain" class="ui-widget">
  185. <table id="html_master" class="ui-widget ui-widget-content">
  186. <thead>
  187. <tr class="ui-widget-header">
  188. <td>ID</td>
  189. <td>Vendor</td>
  190. <td>Buyer ID</td>
  191. <td>POC Name</td>
  192. <td>POC Email</td>
  193. <td>POC Phone</td>
  194. <td>Edit</td>
  195. </tr>
  196. </thead>
  197. <tbody>
  198.  
  199.  
  200. <?php
  201. /* Foreach loop that brings in information to populate table */
  202. foreach ($dbh->query($sql) as $rows){
  203. ?>
  204. <tr id="<?php echo intval ($rows['MR_ID'])?>">
  205. <td class="mr_id" id="<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo intval ($rows['MR_ID'])?></td>
  206. <td class="mr_name" id="mr_name-<?php echo intval ($rows['MR_ID'])?>" name="field" contenteditable="false"><?php echo $rows['MR_Name']?></td>
  207. <td class="buyer_id" id="buy_id<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['Buyer_ID']?></td>
  208. <td class="poc_n" id="poc_n-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_N']?></td>
  209. <td class="poc_e" id="poc_e-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_E']?></td>
  210. <td class="poc_p" id="poc_p-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_P']?></td>
  211. <td><input type="button" class="edit" name="edit" value="Edit">
  212. </tr>
  213. <?php
  214. }
  215. ?>
  216. </tbody>
  217. </table>
  218. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement