【问题标题】:Ajax always gives positive value. How to correct it?Ajax 总是给出积极的价值。如何纠正?
【发布时间】:2014-10-06 09:28:26
【问题描述】:

我的php代码

function MailList()
{
    $this->Form['email'] = $_POST["email"];
    $index = $this->mgr->getMailList($_POST["email"]);
}


//sql code

function getMailList($email)
{           
    $mailArray = Array();
    $sql = "select vchEmail as mailName from tbDownloadDetails  where vchEmail='".$email."'";
    $result = mysql_query($sql, $this->con);
    $isexist=false; 

    while ($row = mysql_fetch_array($result))
    {
        $isexist=true;
        array_push($mailArray, $row['mailName']);
    }       
    return ($mailArray);
}

Ajax 代码

function getMailList(e)
{       

    $value=e;
    $.ajax({
            url:'http://example.com/report/' + "filedownload.html",
            type:"POST",
            data:{action:'MailList',email:$value},
            dataType:"html",
            success:function (resp) {
                if (resp !== '') {
                    alert("Success");
                } else {
                    alert("Fail");
                    return false;
                }
            }
        });
         return false;
}   

这是我的代码。但它不起作用。当我输入电子邮件 ID 时,它总是显示(警报)成功,如果它不存在于 db 中。也就是说,代码总是给出正值。如何更正代码。 提前谢谢....

【问题讨论】:

    标签: javascript php ajax smarty


    【解决方案1】:

    我假设当您调用脚本的操作 MailList 时会调用 getMailList()。

    您正在返回一个数组。如果未找到电子邮件地址,您的脚本将返回一个空数组。尝试检查resp.length === 0 :)

    【讨论】:

      【解决方案2】:

      在您的回复中,您返回的是一个数组。如果电子邮件根本不存在,它将返回一个空数组..

      因此您的回复将是

      [] 而不是 ''。

      在成功回调中更改您的比较。并像这样检查。

      resp.length === 0

      【讨论】:

        【解决方案3】:

        试试这样:

        <?php 
        
        function getMailList($email)
        {           
            $mailArray = Array();
            $sql = "select vchEmail as mailName from tbDownloadDetails  where vchEmail='".$email."'";
            $result = mysql_query($sql, $this->con) or die(mysql_error());
            if(mysql_num_rows($result)>0){
                while ($row = mysql_fetch_array($result))
                {
                    array_push($mailArray, $row['mailName']);
                }   
            }
            else{
                return false;
            }
        
        
            return count($mailArray)>0 ? $mailArray : false;
        }
        
        ?>
        

        Ajax 代码:

        function getMailList(e)
        {       
        
            $value=e;
            $.ajax({
                    url:'http://example.com/report/' + "filedownload.html",
                    type:"POST",
                    data:{action:'MailList',email:$value},
                    dataType:"html",
                    success:function (resp) {
                        if (resp !== false) {
                            alert("Success");
                        } else {
                            alert("Fail");
                            return false;
                        }
                    }
                });
                 return false;
        }
        

        【讨论】:

          【解决方案4】:

          返回一个字符串而不是return 'true'; 或使用=== 进行比较。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-08-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-01-26
            相关资源
            最近更新 更多