php - 从php获取返回值到ajax
问题描述
我正在使用SweetAlert
确认,我无法从我的 php 获取返回数据到 ajax
这就是我正在做的
我的后端 php
//Add A voucher
public function AddVoucher ($voucher_name, $quantity, $discount)
{
try
{
$stmt2 = $this->db->prepare("SELECT * FROM vouchers WHERE voucher_name=:voucher_name");
$stmt2->bindParam(":voucher_name", $voucher_name,PDO::PARAM_STR);
$stmt2->execute();
$stmt2->fetchAll(PDO::FETCH_ASSOC);
if($stmt2->rowCount())
{
return "Failed";
}
else
{
$discount /= 100;
//name doesn't exist so proceed to registration
$stmt = $this->db->prepare("INSERT INTO vouchers (voucher_name,quantity,discount)
VALUES(:voucher_name, :quantity, :discount)");
$stmt->bindParam(":voucher_name", $voucher_name,PDO::PARAM_STR);
$stmt->bindParam(":quantity", $quantity,PDO::PARAM_STR);
$stmt->bindParam(":discount", $discount,PDO::PARAM_INT);
$stmt->execute();
return $stmt;
}
}
catch(PDOException $ex)
{
echo $ex->getMessage();
}
}
我的模型 php
<?php
require_once '../database/database.php';
$voucher_name = $_POST['voucher_name'];
$quantity = $_POST['quantity'];
$discount = $_POST['discount'];
$result = $user->AddVoucher($voucher_name,$quantity,$discount);
if($result == "Failed"):
return false;
else:
return true;
endif
?>
现在我的 ajax 存在的前端
function SwalConfirm()
{
Swal.fire({
title: 'Are you sure?',
text: "You will an a voucher . Do you wish to proceed",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, Proceed',
cancelButtonText: 'No, cancel!',
confirmButtonClass: 'btn bg-gradient-primary w-30',
cancelButtonClass: 'btn bg-gradient-secondary w-30',
buttonsStyling: false,
preConfirm: function()
{
return new Promise(function(resolve){
$.ajax({
url: '../model/voucher_add.php',
type: 'POST',
data:
{
voucher_name: $('#a_voucher_name').val(),
quantity: $('#a_quantity').val(),
discount: $('#a_discount').val()
},
success: function(data)
{
if(!data)
{
Swal.fire('Opps!', 'Voucher Name Already Exists. Please Edit the current voucher instead','warning').then(function(){
location.reload();
});
}
else
{
Swal.fire('Success!', 'You have successfully added a voucher.','success').then(function(){
location.reload();
});
}
}
})
// .done(function(response)
// {
// Swal.fire('Opps!', 'Voucher Name Already Exists. Please Edit the current voucher instead','warning').then(function(){
// location.reload();
// });
// })
.fail(function(){
Swal.fire('Oppss!', 'There was something wrong declining this request.','success')
})
});
}
});
}
我在这里要做的是,如果当前凭证已经存在,则不要继续并使用sweetalert
而不是成功消息显示错误
解决方案
对于那些可能遇到类似问题的人来说,解决方案就是这样
if($stmt2->rowCount())
{
echo "0";
}
然后像这样抓住它给你
if(data == 0)
{
Swal.fire('Opps!', 'Voucher Name Already Exists. Please Edit the current voucher instead','warning').then(function(){
location.reload();
});
}
就是这样。但也要遵循劳伦斯所说的关于捕捉空值的说法
推荐阅读
- c# - var 关键字可以将变量类型更改为动态吗?
- python - 如何正确安装手电筒?
- http - Flutter 中的 HTTP POST 获取 Cookie
- laravel - 重定向到受中间件 laravel 保护的路由
- angular - 如何在前端的 IIS Windows 身份验证中获取用户名?
- python-3.x - 我需要能够打印出星号模式,要求用户输入行数
- typescript - keyof typeof StaticClass 似乎包括 StaticClass' 类型本身
- flutter - 无法在颤振中检索保存的图像(仅在 iOS 上可重现的问题)
- mongodb - Querying nested object arrays
- php - 从 WordPress 小部件获取数据以修改帖子内容