简体   繁体   English

Ajax结果依赖于PHP结果

[英]Ajax result dependant on PHP result

I have a PHP function that gives an response for an alert if the result is > 0 however I am getting an empty alert if the result is 0 我有一个PHP函数,给出了一个警告的响应如果结果> 0 ,如果结果是不过我得到一个空的警报0

How can I stop this? 我怎么能阻止这个?

 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: jQuery Ajax:

 $.ajax({
            url:url,
            data:data ,

            type: 'POST',
            success: function (resp) {
                alert(resp);
            },
            error: function (resp) {
                console.log(data);
            }
        });

That is because you are sending the response only if result > 0.Send another response if your result is <= 0.So write an else case like the following. 这是因为只有当结果> 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 1;
    }
    else{
       echo 0;
    }
}

Ajax 阿贾克斯

 $.ajax({
            url:url,
            data:data ,

            type: 'POST',
            success: function (resp) {
                if(resp){
                  alert('This page name already exists');
                }
            },
            error: function (resp) {
                console.log(data);
            }
        });

try this one: 试试这个:

$.ajax({
            url:url,
            data:data ,

            type: 'POST',
            success: function (resp) {
        if(resp == '')
            //do something
        else
            alert(resp);
            }

        });

You could do another check in the client side: 您可以在客户端进行另一项检查:

Server 服务器

<?php

if ($urlCheck > 0) {
   echo '1 - This page name already exists.';
} else {
   echo '0 - New page name.';
}
?>

Client: 客户:

// success handler
success: function(resp) {
   var realResp = resp.split('-');
   if (realResp[0] == '1') {
      alert(realResp[1]);
   }
}

Try something like this.. 试试这样的东西......

if($urlCheck > 0) {
    echo "This page name already exists";
} else {
    echo -1;
}

Client - 客户 -

success: function (resp) {
    if(resp != -1) {    
        alert(resp);
    }
}

here is a fully working example.. NOTE: since you are using a class I had to improvise with something 这是一个完全有效的例子。注意:因为你正在使用一个我必须即兴创作的课程

<?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>

Here is why I state not to use success: 这就是我说不使用成功的原因:

Deprecation Notice 弃用通知

The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callback methods introduced in jQuery 1.5 are deprecated as of jQuery 1.8. 自jQuery 1.8起,jQuery 1.5中引入的jqXHR.success(),jqXHR.error()和jqXHR.complete()回调方法已弃用。 To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead. 要准备最终删除的代码,请使用jqXHR.done(),jqXHR.fail()和jqXHR.always()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM