2017-02-17 53 views
0

我有一個可點擊的按鈕,可以調出帶有一個文本框的彈出框。每當我輸入內容並點擊「添加」時,我都希望它被插入到我的數據庫中。向數據庫添加使用PHP/JavaScript/AJAX的行

目前,當我點擊「添加」時,它會插入到數據庫中,但它不會讀取輸入的值。因此,只需輸入一個空值。我看不到任何錯誤,但是在JavaScript中,我執行了console.log(type + " : " + value);並在日誌中返回sku_group-0 :。我也做了一個console.log(dict)和在日誌中的輸出是Object {},所以它看起來不像輸入的值被記錄。我也在日誌中獲得了成功的row inserted消息,所以它看起來確實看起來像是能夠讀取值以便可以在insert-group.php腳本中處理這些值。

所以我的問題是我怎樣才能讓它讀取JavaScript中的值,以便它可以成功地進入數據庫?彈出框的

HTML:

<div id="dialog-form" title="Add Group"> 
    <p class="validateTips">Please Add a Group</p> 

<!-- Dialog box displayed after add row button is clicked --> 
    <form> 
    <fieldset>   

     <label for="sku_group">SKU Group</label> 
     <input type="text" name="group" id="group" class="text ui-widget-content ui-corner-all"> 


     <!-- Allow form submission with keyboard without duplicating the dialog button --> 
     <input type="submit" id="submit" tabindex="-1" style="position:absolute; top:-1000px"> 
    </fieldset> 
    </form> 
</div> 

HTML添加按鈕:

<button class="create-user" id="insertButton">Add Group</button> 

的JavaScript:

$(function() { 

    var dialog, form, 

     sku_group = $("#group"), 
     allFields = $([]).add(sku_group), 
     tips = $(".validateTips"); 
    console.log(allFields); 

    function updateTips(t) { 
     tips 
     .text(t) 
     .addClass("ui-state-highlight"); 
     setTimeout(function() { 
     tips.removeClass("ui-state-highlight", 1500); 
     }, 500); 
    } 

    function checkRegexp(o, regexp, n) { 
     if (!(regexp.test(o.val()))) { 
     o.addClass("ui-state-error"); 
     updateTips(n); 
     return false; 
     } else { 
     return true; 
     } 
    } 


    function addGroup() { 

     var valid = true; 
     allFields.removeClass("ui-state-error"); 
// ----- Validation for each input in add row dialog box ----- 
     valid = valid && checkRegexp(sku_group, /^[a-z]([0-9a-z_\s])+$/i, "Please enter a valid SKU Group name"); 
     console.log(allFields); 
     if (valid) { 
     var $tr = $("#skuTable tbody tr td").eq(0).clone(); 
     var dict = {}; 
     var errors = ""; 
     $tr.each(function(){ 
     var type = $(this).attr('id'); 
     var value = $(this).html(); 
     console.log(type + " : " + value); 

     switch (type) { 
     case "group": 
      dict["SKU Group"] = value; 
     break; 
     } 
    }); 
     $("#skuTable tbody").append($tr); 
     dialog.dialog("close"); 
     console.log(dict); 


     var request = $.ajax({ 
      type: "POST", 
      url: "insert-group.php", 
      data: dict 
     }); 

     request.done(function (response, textStatus, jqXHR){ 
      if(JSON.parse(response) == true){ 
      console.log("row inserted"); 
      } else { 
      console.log("row failed to insert"); 
      console.log(response); 
      } 
     }); 

     // Callback handler that will be called on failure 
     request.fail(function (jqXHR, textStatus, errorThrown){ 
      console.error(
       "The following error occurred: "+ 
       textStatus, errorThrown 
      ); 
     }); 

     // Callback handler that will be called regardless 
     // if the request failed or succeeded 
     request.always(function() { 

     }); 

} 

     return valid; 
    } 

    var dialog = $("#dialog-form").dialog({ 
     autoOpen: false, 
     height: 400, 
     width: 350, 
     modal: true, 
     buttons: { 
     "Add Group": addGroup, 
     Cancel: function() { 
      dialog.dialog("close"); 
     } 
     }, 
     close: function() { 
     form[ 0 ].reset(); 
     } 
    }); 

    form = dialog.find("form").on("submit", function(event) { 
     event.preventDefault(); 
     addGroup(); 
    }); 

    $(".create-user").button().on("click", function() { 
     dialog.dialog({ 
      show: 'blind', 
      hide: 'blind' 
     }); 
     dialog.dialog("open"); 
    }); 

    }); 

刀片式group.php腳本:

<?php 

    $SKU_Group = $_POST['SKU Group']; 

    $host="xxxxxxxxxxx"; 
    $dbName="xxxxxxx"; 
    $dbUser="xxxx"; 
    $dbPass="xxxxxxxxxxxxxx"; 

    $pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass); 

    $sql = "INSERT INTO SKU_Group_Dim ([SKU Group]) VALUES (?)"; 

    $stmt = $pdo->prepare($sql); 
    $result = $stmt->execute(array($SKU_Group)); 
    echo json_encode($result); 

?> 
+0

嘗試使用返回false;而不是event.preventDefault(); - 聽起來就像你的表單文章正在發生。 – Paul

+0

@Paul在使用'return false;'時我在日誌中獲得了相同的輸出結果' – Rataiczak24

+0

您是否嘗試過使用loggin dict,logging dict [「SKU Group」]。似乎它的顯示是一個對象,所以它找到它。此外,我不能對你是什麼 –

回答

2
REPLACE 
    "data: dict" 
    WITH 
    data:{ 'SKU_Group' : $('#group').val() } 

    AND 

    REPLACE 
    "$SKU_Group = $_POST['SKU Group'];" 
    WITH 
    $SKU_Group = $_POST['SKU_Group']; 
0

你應該讓你的輸入值:

$('#group').val()