2017-02-16 71 views
0

我有一个按钮,可以单击将会弹出一个弹出框与一个文本框。每当我输入内容并单击“添加”时,我都希望它被插入到我的数据库中。使用JavaScript/AJAX来插入行数据库

目前,当我点击“添加”,它会插入到数据库,但它不会被读取输入的值。因此,只需输入一个空值。我看不到任何错误,但是在JavaScript中,我执行了console.log(type + " : " + value);并在日志中返回sku_group-0 :。我也做了一个console.log(dict)和在日志中的输出是Object {},所以它看起来不像输入的值被记录。我也在日志中获得成功的row inserted消息,所以我肯定会认为它只是能够获取值的读取问题。

所以我的问题是我怎么能得到它的阅读价值,并成功将其输入到数据库中。

HTML的添加按钮:

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

弹出的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> 

的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

INSERT INTO不返回插入的值。更改php并创建第二个mysql请求以获取新数据。想想同样的条目(唯一标识符)。 – Pete

回答

1

修改声明

sku_group = $("#group") 

sku_group = $("#group").val(); 

这应该解决您的问题。

编辑保存TxtValue到数据库...

$(function() { 
     var TxtValue = $("#group").val(); 

     $("#submit").click(function (e) { 

       e.preventDefault(); 
      if(!TxtValue){ 
       $.ajax({ 
        type: "POST", 
        url: "insert-group.php", 
        data: {[agrumentName]: TxtValue}, 
        success: function(e){ 
         console.log(e); 
         console.log("Row Insrerted"); 
        } 
       }); 
      } 
      else{ 
       console.log("Cannot Insert Null Value"); 
      } 
     }) 
    }) 

与您的服务器函数的参数替换[agrumentName。

希望这有助于:)

+0

我现在得到一个错误'o.val'是不是在我的'功能checkRegexp' – Rataiczak24

+0

的功能,当你已经从传递价值“$(‘#组’)。VAL()”,你需要不适用VAL ()到O,你可以直接与邻去,而不是o.val() – user7417866

+0

我得到O'后'摆脱'.VAL()',并得到另一个错误是'o.addClass'不是一个功能,所以为了测试的目的,我评论了它,但它仍然不起作用。仍然插入一个NULL值。 – Rataiczak24