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);
?>
值被循环到下拉列表中,并且有200多个,所以在那里一种为所有人做几行代码的方法?这种方法似乎我可能必须硬编码所有内容,我可能是错的,但那是我如何阅读它 – Rataiczak24
你会做这只是在选定的值的职位之前。没有必要对他们做所有的事情。 –
你能够更新答案,并包括我的一些JavaScript,所以我知道到底把它放在哪里? – Rataiczak24