如何避免在PHP中的警报框

时间:2014-12-17 04:55:57

标签: javascript php jquery ajax

我希望只有当我没有回复时才会显示警报。当我没有给出回声时我会显示警告框。我想在“如果'之后显示警告”。条件。

 $.ajax({
 type:'post',
url:'email.php',
data:{email: email},
success:function(msg){
alert(msg);         
}
});

email.php

$s=$_POST['email'];
include "config.php";
$echeck="select email from register where email='".$_POST['email']."'";
$echk=mysql_query($echeck);
$ecount=mysql_num_rows($echk);
if($ecount>=1 && $s!='0')
{
  echo "Email already exists";
}

4 个答案:

答案 0 :(得分:3)

尝试

$.ajax({
  type:'post',
  url:'email.php',
  data:{email: email},
  success:function(msg){
      if($.trim(msg) != '') {
          alert(msg);        
      } 
  }
});

您也可以使用Length

msg = $.trim(msg);
if(msg.length > 0) {
    alert(msg);
}

答案 1 :(得分:2)

检查消息长度

success:function(msg){
 if (msg.length> 0) {
  alert(msg);
 }
}

Reference

<强>解释

有两个条件:

1)你正在回应一些事情。

2)你没有回应什么。

在第一种情况下,您将获得msg的长度大于0

在第二种情况下,您将获得0的长度,因此,不会出现更改。

答案 2 :(得分:1)

使用msg长度轻松实现。

if(msg.length&gt; 0)

答案 3 :(得分:1)

因为msg变量中有空格:

尝试以下方法: var string = msg.replace(/ + / g,&#34;&#34;);

如果(字符串=&#34;!&#34;){  警报(MSG);

}