获取从 php 到 ajax 的 return 值
Get return value from php to ajax
我正在使用 SweetAlert
进行确认,但我无法将 return 数据从我的 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";
}
然后像这样ajax抓给你
if(data == 0)
{
Swal.fire('Opps!', 'Voucher Name Already Exists. Please Edit the current voucher instead','warning').then(function(){
location.reload();
});
}
就是这样。但也要遵循劳伦斯所说的关于捕获 null
的内容
型号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 json_encode(false);
else:
return json_encode(true);
endif
?>
它将 return json_encoded 结果为 ajax
我正在使用 SweetAlert
进行确认,但我无法将 return 数据从我的 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";
}
然后像这样ajax抓给你
if(data == 0)
{
Swal.fire('Opps!', 'Voucher Name Already Exists. Please Edit the current voucher instead','warning').then(function(){
location.reload();
});
}
就是这样。但也要遵循劳伦斯所说的关于捕获 null
的内容型号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 json_encode(false);
else:
return json_encode(true);
endif
?>
它将 return json_encoded 结果为 ajax