【问题标题】:When I try to check if email exists jQuery doesn't give the error?当我尝试检查电子邮件是否存在时,jQuery 没有给出错误?
【发布时间】:2014-01-01 15:49:15
【问题描述】:

我正在尝试检查数据库中是否存在电子邮件,但是当我这样做时,jQuery 不会显示已经存在的消息。

错误出现在 if 语句 "if(cemail == 1)" 中。

代码:

 <script type="text/javascript">
    $(document).ready(function() {

        $("#sub").submit(function() {

            if($("#username").val()==""||$("#email").val()=="") {
                $("#warn2").hide(36000);
                $("#loading").css("display:inline");
                $("#loading").html("<img src='images/ajax-loader.gif' width='1%' height='3%' /> A carregar...");
                $("#loading").fadeIn(200);
                $("#loading").fadeOut(200);
                $("#warn2").css("display:none");
                $("#warn2").html("Por favor escreve o teu nome e/ou email!<br />");
                $("#warn2").hide(36000);
                $("#warn2").fadeIn(1000);                   
                $("#warn2").css("display:inline");
                $("#warn2").fadeOut(7000);

            }

            else {                                         


                var s = $("#sub").serializeArray();
                $.ajax({

                    success: function(cemail) {

                            if(cemail == 1) {
                            $.ajax ({ url: "check-email.php", method: "POST", data: "" });  
                            $("#warn2").hide(36000);
                            $("#loading").css("display:inline");
                            $("#loading").html("<img src='images/ajax-loader.gif' width='1%' height='3%' /> A carregar...");
                            $("#loading").fadeIn(200);
                            $("#loading").fadeOut(200);
                            $("#warn2").css("display:none");
                            $("#warn2").html("Esse E-mail j&aacute; se encontra na nossa base de dados!<br />");
                            $("#warn2").hide(36000);
                            $("#warn2").fadeIn(1000);                   
                            $("#warn2").css("display:inline");
                            $("#warn2").fadeOut(7000);
                        } else {
                var email = $("#email").val();
                                  if(isValid(email)==false){
                    $("#warn2").hide(36000);
                    $("#loading").css("display:inline");
                    $("#loading").html("<img src='images/ajax-loader.gif' width='1%' height='3%' /> A carregar...");
                    $("#loading").fadeIn(200);
                    $("#loading").fadeOut(200);
                    $("#warn2").css("display:none");
                    $("#warn2").html("Por favor introduza um e-mail v&aacute;lido!<br />");
                    $("#warn2").hide(36000);
                    $("#warn2").fadeIn(1000);                   
                    $("#warn2").css("display:inline");
                    $("#warn2").fadeOut(7000);
                    $.ajax ({ url: "", method: "POST", data: "" });
                }
                     else {

                            $("#success").css("display:none");              
                            $("#success").hide(36000);
                            $("#loading").css("display:inline");
                            $("#loading").html("<img src='images/ajax-loader.gif' width='1%' height='3%' /> A carregar...");
                            $("#loading").fadeIn(200);
                            $("#loading").fadeOut(200);
                            $("#success").css("display:none");
                            $("#success").html("A tua subscri&ccedil;&atilde;o foi efectuada com successo!");
                            $("#success").hide(36000);
                            $("#success").fadeIn(1000);                 
                            $("#success").css("display:inline");
                            $("#success").fadeOut(7000);
                            $.ajax ({ url: "subscribe.php",
                            method: "POST",
                            data: s,});
                            $("#username").val("");
                            $("#email").val("");
                            return true;
                        }

                 }

            }


            });


 function isValid(email) {
        var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if(!regex.test(email)) {
           return false;
        }else{
           return true;
        }
      }
            }
        return false;




    });

        $("#MGa").click(function() {

            $("#hidGames").css("display: inline");
            $("#hidGames").fadeToggle(1000);


            return false;
        });

        $("#MGa").mouseover(function() {

            $("#arrow").html('<img src="images/arrow_wbm_hover.png" width="1%" height="1%" />');


        });

        $("#MGa").mouseout(function() {

            $("#arrow").html('<img src="images/arrow.png" width="1%" height="1%" />');


        });

        $("#eqp_tec").click(function() {

            $("eqpTec").css("display: inline");
            $("#eqpTec").fadeToggle(1000);

            return false;

        });


        $("#arrow").html('<img src="images/arrow.png" width="1%" height="1%" />');

    });
</script>

这里是php页面(check-email.php):

<?php

include("db.php");

$email = $_POST['email'];


$cemail = mysql_query("SELECT * FROM subs WHERE email='$email'");

$rows = mysql_num_rows($cemail);

if($rows==1) {
    echo "User in database";
} else {
    // whatever
}

?>

你能帮我解决这个错误吗?我真的需要它。 如果这个错误继续存在,我会比现在更头疼。帮我解决这个错误。 谢谢!

【问题讨论】:

    标签: php jquery email


    【解决方案1】:

    这里有几个问题(其中最重要的是你有一些严重过度编写的代码)

    首先,您有两个ajax 函数调用,一个包装另一个。

    $.ajax({
        success: function(cemail) {
            if(cemail == 1) {
    

    我什至不确定第一个会做什么,因为它没有指定 URL。这看起来是一个主要问题,但我不知道如果您在没有 URL 的情况下调用 JQ AJAX 会发生什么。你真的需要在这里阅读jQuery AJAX documentation 并可能删除第一个ajax 调用。

    第二,你在重复自己。 很多。编码的核心原则是

    干燥 - 不要重复自己

    这就是函数的用途。他们为你做你的重复。这使您的代码更清晰。所以这里有一个你需要添加的示例函数。

    function showError(error) {
        $("#warn2").hide(36000);
        $("#loading").css("display:inline");
        $("#loading").html("<img src='images/ajax-loader.gif' width='1%' height='3%' /> A carregar...");
        $("#loading").fadeIn(200);
        $("#loading").fadeOut(200);
        $("#warn2").css("display:none");
        $("#warn2").html(error);
        $("#warn2").hide(36000);
        $("#warn2").fadeIn(1000);                   
        $("#warn2").css("display:inline");
        $("#warn2").fadeOut(7000);
    }
    

    第三,您使用的是mysql_query。单击该链接并阅读红色框。您需要停止使用这些函数,因为它们可能会从 PHP 中删除。

    第四,您的 PHP 对 SQL 注入是开放的。这是一个handy page,告诉您如何确保安全。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多