Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ----- Edit Row -----
- $(document).on("click", "#skuTable .edit", function () {
- var $this = $(this);
- var tds = $this.closest('tr').find('td').filter(function () {
- return $(this).find('.edit').length === 0;
- });
- if ($this.val() === 'Edit') {
- $this.val('Save');
- if($this.id !== '.major_cat') {
- 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);
- }
- } else {
- var isValid = true;
- var errors = '';
- $('#myDialogBox').empty();
- var elements = tds;
- if (tds.find('input').length > 0) {
- elements = tds.find('input');
- }
- var dict = {};
- elements.each(function (index, element) {
- var type = $(this).attr('class');
- var value = (element.tagName == 'INPUT') ? $(this).val() : $(this).text();
- console.log(type);
- // ----- Switch statement that provides validation for each table cell -----
- switch (type) {
- case "sku":
- dict["SKU"] = value;
- break;
- case "group_id":
- if (!$.isNumeric(value)) {
- isValid = false;
- errors += "Please enter a numeric Group IDn";
- }
- if (isValid) {
- dict["Group_ID"] = value.trim();
- }
- break;
- /*case "sku_group":
- dict["SKU Group"] = value.trim();
- break;*/
- }
- })
- if (isValid) {
- console.log(dict);
- $this.val('Edit');
- tds.prop('contenteditable', false);
- if(confirm("Saving will insert or update the data. Do you wish to continue?") == true) {
- var request = $.ajax({
- type: "POST",
- url: "update-index.php",
- data: dict
- });
- request.done(function (response, textStatus, jqXHR){
- if(JSON.parse(response) == true){
- console.log("row inserted/updated");
- alert("Row inserted/updated successfully");
- } else {
- console.log("row failed to updated");
- alert("The row failed to insert/update");
- console.log(response);
- console.log(textStatus);
- console.log(jqXHR);
- }
- });
- // Callback handler that will be called on failure
- request.fail(function (jqXHR, textStatus, errorThrown){
- // Log the error to the console
- console.log(textStatus);
- console.log(jqXHR);
- console.error(
- "The following error occurred: "+
- textStatus, errorThrown
- );
- });
- // Callback handler that will be called regardless
- // if the request failed or succeeded
- request.always(function () {
- });
- } else {
- alert("The entry was not saved");
- } // ends if statement with confirmation box
- } else {
- alert(errors);
- }
- }
- });
- $Group_ID = $_POST['Group_ID'];
- $SKU = $_POST['SKU'];
- $host="xxxxxxxxx";
- $dbName="xxxxxx";
- $dbUser="xxxxxxxxxxxxxx";
- $dbPass="xxxx";
- $pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);
- $sql = "IF EXISTS (SELECT SKU FROM SKU_Group_Index WHERE SKU = ?)
- AND EXISTS (SELECT Group_ID FROM SKU_Group_Dim WHERE Group_ID = ?)
- BEGIN
- UPDATE SKU_Group_Index
- SET Group_ID = ?
- WHERE SKU = $SKU
- END
- ELSE
- BEGIN
- IF EXISTS (SELECT Group_ID FROM SKU_Group_Dim WHERE Group_ID = ?)
- BEGIN
- INSERT INTO SKU_Group_Index (SKU, Group_ID) VALUES (?, ?)
- END
- END";
- $stmt = $pdo->prepare($sql);
- $result = $stmt->execute([$SKU, $Group_ID, $Group_ID, $Group_ID, $SKU, $Group_ID]);
- echo json_encode($result);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement