>我有一个PHP函数,如果结果> 0
,它会响应警报,但是如果结果是0
,我会收到一个空警报
我怎样才能阻止这种情况?
function urlCheck()
{
$website_id = $this->input->post('website_id');
$post_title = $this->input->post('post_title');
$urlCheck = $this->page_model->pageURLCheck($post_title, $moviesparx_website_id);
if($urlCheck > 0)
{
echo "This page name already exists";
}
}
jQuery Ajax:
$.ajax({
url:url,
data:data ,
type: 'POST',
success: function (resp) {
alert(resp);
},
error: function (resp) {
console.log(data);
}
});
这是因为只有在结果> 0 时才发送响应。如果结果为 <= 0.So 编写如下所示的 else 案例,则发送另一个响应。
function urlCheck()
{
$website_id = $this->input->post('website_id');
$post_title = $this->input->post('post_title');
$urlCheck = $this->page_model->pageURLCheck($post_title, $moviesparx_website_id);
if($urlCheck > 0)
{
echo 1;
}
else{
echo 0;
}
}
阿贾克斯
$.ajax({
url:url,
data:data ,
type: 'POST',
success: function (resp) {
if(resp){
alert('This page name already exists');
}
},
error: function (resp) {
console.log(data);
}
});
试试这个:
$.ajax({
url:url,
data:data ,
type: 'POST',
success: function (resp) {
if(resp == '')
//do something
else
alert(resp);
}
});
您可以在客户端进行另一次检查:
服务器
<?php
if ($urlCheck > 0) {
echo '1 - This page name already exists.';
} else {
echo '0 - New page name.';
}
?>
客户:
// success handler
success: function(resp) {
var realResp = resp.split('-');
if (realResp[0] == '1') {
alert(realResp[1]);
}
}
试试这样的事情。
if($urlCheck > 0) {
echo "This page name already exists";
} else {
echo -1;
}
客户-
success: function (resp) {
if(resp != -1) {
alert(resp);
}
}
这是一个完全工作的例子。注意:由于您使用的是类,因此我不得不即兴创作一些东西
<?php
function urlCheck ($urlCheck)
{
if ($urlCheck > 0) echo "This page name already exists"; exit;
}
if (isSet ($_POST ['send']))
{
urlCheck (1);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>blah</title>
<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready (function (){
$.ajax({
url: "index.php",
data: {send: "data"},
type: 'POST',
}).fail (function () {
console.log ("failed");
}).done (function (data){ //dont use success
alert (data);
});
});
</script>
</head>
<body>
</body>
</html>
这就是为什么我声明不使用成功:
弃用通知
jqXHR.success()、jqXHR.error() 和 jqXHR.complete() 回调 jQuery 1.5 中引入的方法从 jQuery 1.8 开始被弃用。自 准备代码以最终删除它们,使用 jqXHR.done(), jqXHR.fail() 和 jqXHR.always() 代替。