使用 JSON 检查确认消息后的任何数据
Check any data after confirmation message using JSON
我想在用户选择确认消息后将数据警报设置为成功保存或失败。在这种情况下,数据检查发生在用户确认我发出的消息之后。
这是我的Javascript代码:
$('#add-btn').on('click', function(event){
var confirmation = confirm("Do you want to save ?");
if (confirmation == true) {
var code = $('#code').val();
var name = $('#name').val();
var unit = $('#unit').val();
var price_code = $('#price_code').val();
var type = $('#type').val();
var final = $('#final').val();
var dataString = 'code=' +code+ '&unit=' +unit+ '&price_code=' +price_code+ '&type=' +type;
if (code != '' && name != '' && unit != '' && price_code != '' && type != '' && final != ''){
event.preventDefault();
$.ajax({
type: "POST",
url: "../public/process/add_data.php",
data: dataString,
dataType: "json",
cache: false,
success: function(data){
if(data.status == 'success'){
alert("Success !");
}
else if(data.status == 'error'){
alert("Data already used !");
}
}
});
}
else{
alert('Please fill all fields !');
}
}
});
所有输入成功保存但提示无法显示
我认为您的 php 文件有问题。您的 JSON 数据格式与您在 success.Please 中收到的格式不正确 在您的 add_data.php 文件中试试这个
//All code goes here and after insert try this
$array = array();
if(if data insert successfully) {
$array['status '] = 'success';
} else {
$array['status '] = 'error';
}
header('Content-Type: application/json');
echo json_encode($array);
成功功能只有在一切顺利的情况下才会执行。如果出现任何错误,您需要添加 Ajax 失败函数,如下所示:
$.ajax({
type: "POST",
url: "../public/process/add_data.php",
data: dataString,
dataType: "json",
cache: false,
success: function(data){
alert("Success !");
}
}).fail(function () {
alert("Data already used !");
});
我终于找到了解决办法。我为我的粗心感到抱歉。
发生这种情况是因为我的变量 dataString 没有完成。
应该是:
var dataString = 'code=' +code+ '&name=' +name+ '&unit=' +unit+
'&price_code=' +price_code+ '&type=' +type+ '&final=' +final;
谢谢大家的好意:-)
我想在用户选择确认消息后将数据警报设置为成功保存或失败。在这种情况下,数据检查发生在用户确认我发出的消息之后。
这是我的Javascript代码:
$('#add-btn').on('click', function(event){
var confirmation = confirm("Do you want to save ?");
if (confirmation == true) {
var code = $('#code').val();
var name = $('#name').val();
var unit = $('#unit').val();
var price_code = $('#price_code').val();
var type = $('#type').val();
var final = $('#final').val();
var dataString = 'code=' +code+ '&unit=' +unit+ '&price_code=' +price_code+ '&type=' +type;
if (code != '' && name != '' && unit != '' && price_code != '' && type != '' && final != ''){
event.preventDefault();
$.ajax({
type: "POST",
url: "../public/process/add_data.php",
data: dataString,
dataType: "json",
cache: false,
success: function(data){
if(data.status == 'success'){
alert("Success !");
}
else if(data.status == 'error'){
alert("Data already used !");
}
}
});
}
else{
alert('Please fill all fields !');
}
}
});
所有输入成功保存但提示无法显示
我认为您的 php 文件有问题。您的 JSON 数据格式与您在 success.Please 中收到的格式不正确 在您的 add_data.php 文件中试试这个
//All code goes here and after insert try this
$array = array();
if(if data insert successfully) {
$array['status '] = 'success';
} else {
$array['status '] = 'error';
}
header('Content-Type: application/json');
echo json_encode($array);
成功功能只有在一切顺利的情况下才会执行。如果出现任何错误,您需要添加 Ajax 失败函数,如下所示:
$.ajax({
type: "POST",
url: "../public/process/add_data.php",
data: dataString,
dataType: "json",
cache: false,
success: function(data){
alert("Success !");
}
}).fail(function () {
alert("Data already used !");
});
我终于找到了解决办法。我为我的粗心感到抱歉。 发生这种情况是因为我的变量 dataString 没有完成。 应该是:
var dataString = 'code=' +code+ '&name=' +name+ '&unit=' +unit+
'&price_code=' +price_code+ '&type=' +type+ '&final=' +final;
谢谢大家的好意:-)