Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. // ----- Edit Row -----
  2.  
  3. $(document).on("click", "#skuTable .edit", function () {
  4. var $this = $(this);
  5. var tds = $this.closest('tr').find('td').filter(function () {
  6. return $(this).find('.edit').length === 0;
  7. });
  8. if ($this.val() === 'Edit') {
  9. $this.val('Save');
  10. if($this.id !== '.major_cat') {
  11. tds.not('.major_cat').not('.minor_cat').not('.rep_code').not('.sku_desc').not('.sku_status').not('.create_date').not('.sku').not('.sku_group').prop('contenteditable', true);
  12.  
  13. }
  14. } else {
  15. var isValid = true;
  16. var errors = '';
  17. $('#myDialogBox').empty();
  18. var elements = tds;
  19. if (tds.find('input').length > 0) {
  20. elements = tds.find('input');
  21. }
  22. var dict = {};
  23. elements.each(function (index, element) {
  24. var type = $(this).attr('class');
  25. var value = (element.tagName == 'INPUT') ? $(this).val() : $(this).text();
  26. console.log(type);
  27. // ----- Switch statement that provides validation for each table cell -----
  28. switch (type) {
  29. case "sku":
  30. dict["SKU"] = value;
  31. break;
  32. case "group_id":
  33. if (!$.isNumeric(value)) {
  34. isValid = false;
  35. errors += "Please enter a numeric Group IDn";
  36. }
  37. if (isValid) {
  38. dict["Group_ID"] = value.trim();
  39. }
  40. break;
  41. /*case "sku_group":
  42. dict["SKU Group"] = value.trim();
  43. break;*/
  44. }
  45. })
  46. if (isValid) {
  47. console.log(dict);
  48. $this.val('Edit');
  49. tds.prop('contenteditable', false);
  50. if(confirm("Saving will insert or update the data. Do you wish to continue?") == true) {
  51. var request = $.ajax({
  52. type: "POST",
  53. url: "update-index.php",
  54. data: dict
  55. });
  56.  
  57. request.done(function (response, textStatus, jqXHR){
  58. if(JSON.parse(response) == true){
  59. console.log("row inserted/updated");
  60. alert("Row inserted/updated successfully");
  61. } else {
  62. console.log("row failed to updated");
  63. alert("The row failed to insert/update");
  64. console.log(response);
  65. console.log(textStatus);
  66. console.log(jqXHR);
  67. }
  68. });
  69.  
  70.  
  71. // Callback handler that will be called on failure
  72. request.fail(function (jqXHR, textStatus, errorThrown){
  73. // Log the error to the console
  74. console.log(textStatus);
  75. console.log(jqXHR);
  76. console.error(
  77. "The following error occurred: "+
  78. textStatus, errorThrown
  79. );
  80. });
  81. // Callback handler that will be called regardless
  82. // if the request failed or succeeded
  83. request.always(function () {
  84.  
  85. });
  86. } else {
  87. alert("The entry was not saved");
  88. } // ends if statement with confirmation box
  89. } else {
  90. alert(errors);
  91. }
  92. }
  93. });
  94.  
  95. $Group_ID = $_POST['Group_ID'];
  96. $SKU = $_POST['SKU'];
  97.  
  98. $host="xxxxxxxxx";
  99. $dbName="xxxxxx";
  100. $dbUser="xxxxxxxxxxxxxx";
  101. $dbPass="xxxx";
  102.  
  103. $pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);
  104.  
  105. $sql = "IF EXISTS (SELECT SKU FROM SKU_Group_Index WHERE SKU = ?)
  106. AND EXISTS (SELECT Group_ID FROM SKU_Group_Dim WHERE Group_ID = ?)
  107. BEGIN
  108. UPDATE SKU_Group_Index
  109. SET Group_ID = ?
  110. WHERE SKU = $SKU
  111. END
  112. ELSE
  113. BEGIN
  114. IF EXISTS (SELECT Group_ID FROM SKU_Group_Dim WHERE Group_ID = ?)
  115. BEGIN
  116. INSERT INTO SKU_Group_Index (SKU, Group_ID) VALUES (?, ?)
  117. END
  118. END";
  119.  
  120. $stmt = $pdo->prepare($sql);
  121. $result = $stmt->execute([$SKU, $Group_ID, $Group_ID, $Group_ID, $SKU, $Group_ID]);
  122. echo json_encode($result);
  123.  
  124.  
  125. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement