2016-12-15 68 views
1

我有一個對話框,在點擊添加按鈕後彈出。有兩個輸入,一個是下拉菜單。例如下拉菜單中的信息如下所示:1 - 公司A,2 - 公司B等。它是由2個連接值組成的值。我需要MR_ID,而不是MR_Name。然而,無論何時我提交併插入數據庫,我只需要這些前幾個數字,而不是公司名稱或任何類似的數字。我怎樣才能做到這一點?將使用str_replace或preg_replace工作?如果是這樣,我將如何以及在哪裏將它放入我的代碼中?取一個字符串並從該字符串中只插入數字

查詢填充下拉列表對話框內

$sql1 = "WITH cte AS (
SELECT DISTINCT CONCAT(CAST(Stage_Rebate_Index.MR_ID AS INT),' - ', Stage_Rebate_Master.MR_Name) AS MR_ID 
    , Stage_Rebate_Index.MR_ID AS sort_column 
FROM Stage_Rebate_Index 
LEFT JOIN Stage_Rebate_Master 
ON Stage_Rebate_Master.MR_ID=Stage_Rebate_Index.MR_ID 
) 
SELECT MR_ID 
FROM 
    cte 
ORDER BY 
    sort_column;"; 

的JavaScript對話框和添加信息:在Java腳本只是用

$(function() { 


    $("#insertButton").on('click', function(e){ 
    e.preventDefault(); 
    }); 

    var dialog, form, 

     mr_id_dialog = $("#mr_id_dialog"), 
     supplier_id = $("#supplier_id"), 
     allFields = $([]).add(mr_id_dialog).add(supplier_id), 
     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 addVendor() { 
     var valid = true; 
     allFields.removeClass("ui-state-error"); 
// ----- Validation for each input in add row dialog box ----- 
     //valid = valid && checkRegexp(mr_id_dialog, /^(0|[1-9][0-9]*)$/, "Please enter a valid MR ID"); 
     valid = valid && checkRegexp(supplier_id, /^(0|[1-9][0-9]*)$/, "Please enter a valid Supplier ID"); 
     console.log(allFields); 
     if (valid) { 
     var $tr = $("#index_table tbody tr").eq(0).clone(); 
     var dict = {}; 
     var errors = ""; 
     $.each(allFields, function(){ 
      $tr.find('.' + $(this).attr('id')).html($(this).val()+"-"+supplier_id); 
      var type = $(this).attr('id'); 
      var value = $(this).val(); 
      console.log(type + " : " + value); 
      // ----- Switch statement that provides validation for each table cell ----- 
      switch (type) { 
      case "mr_id_dialog": 
       dict["MR_ID"] = value; 
       break; 
      case "supplier_id": 
       dict["Supp_ID"] = value; 
       break; 
      } 
     }); 
     $("#index_table tbody").append($tr); 
     dialog.dialog("close"); 
     console.log(dict); 

     var request = $.ajax({ 
      type: "POST", 
      url: "insert.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 Supplier ID": addVendor, 
     Cancel: function() { 
      dialog.dialog("close"); 
     } 
     }, 
     close: function() { 
     form[ 0 ].reset(); 
     allFields.removeClass("ui-state-error"); 
     } 
    }); 

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

    $("#insertButton").button().on("click", function() { 
     dialog.dialog({ 
      position: ['center', 'top'], 
      show: 'blind', 
      hide: 'blind' 
     }); 
     dialog.dialog("open"); 
    }); 
}); 

Insert.php

<?php 

    $MR_ID = $_POST['MR_ID']; 
    $Supp_ID = $_POST['Supp_ID']; 

    $host="xxxxxxxxx"; 
    $dbName="xxxxx"; 
    $dbUser="xxxxxxxxxxxx"; 
    $dbPass="xxxxxxxxx"; 

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

    $sql = "INSERT INTO Stage_Rebate_Index (MR_ID, Supp_ID) VALUES (?, ?)"; 
    $stmt = $pdo->prepare($sql); 
    $result = $stmt->execute(array($MR_ID, $Supp_ID)); 
    echo json_encode($result); 

?> 

回答

2

解析int

parseInt(string); 

​​

這樣可得到2

的jsfiddle https://jsfiddle.net/esz5jnec/

輸出,如果我正確地理解你的代碼,將在開關去

switch (type) { 
    case "mr_id_dialog": 
     dict["MR_ID"] = parseInt(value); 
     break; 
    case "supplier_id": 
     dict["Supp_ID"] = value; 
     break; 
} 
+0

值被循環到下拉列表中,並且有200多個,所以在那裏一種爲所有人做幾行代碼的方法?這種方法似乎我可能必須硬編碼所有內容,我可能是錯的,但那是我如何閱讀它 – Rataiczak24

+0

你會做這只是在選定的值的職位之前。沒有必要對他們做所有的事情。 –

+0

你能夠更新答案,幷包括我的一些JavaScript,所以我知道到底把它放在哪裏? – Rataiczak24