2

目前我可以在我的时事通讯表中添加一个新的电子邮件地址,但是我正在努力处理查询的 AJAX 部分,即。验证。

下面是我的 Signup.php 文件:

<?php

require_once('required/init.php');
    require_once('required/settings.php');
    require_once('required/database.php');
    require_once('required/class.phpmailer.php');
    require_once('required/globals.php');
    $email  = trim($_REQUEST["email"]);
    //  Check if subscriber exists
    $SQL= "select email from tblnewsletter where email='".$email."'";
    $result = mysql_query($SQL);
    if(!$result) {die('Problem in SQL: '.$SQL);}    //just checking if there was a problem with your query

    if (mysql_num_rows($result)==1) {   // he was subscribed already
        echo 'You are subscribed.';         // Change the message if you want.
    }
    else {          // does not exist ==> add to the table
        $SQL2= "INSERT into tblnewsletter (email) VALUES ('".$email."')";
        mysql_query($SQL2);
        echo 'Thank you for subscribing';   // Change the message if you want.
    }

?>

这是我的Javascript:

<script type="text/javascript" language="javascript">
    $(document).ready(function() {
        $('#nlsubmit').on('click', function() {
            signup();
            return false;
        });
    });


    function trim(str) {
        str = str.replace(/^\s*$/, '');
        return str;
    }

    function signup()
    {
        var emailentered = $("#email").val();
        var email = trim(emailentered);

        //EMAIL VALIDATION
        var goodEmail = email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.info)|(\.sex)|(\.biz)|(\.aero)|(\.coop)|(\.museum)|(\.name)|(\.pro)|(\.arpa)|(\.asia)|(\.cat)|(\.int)|(\.jobs)|(\.tel)|(\.travel)|(\.xxx)|(\..{2,2}))$)\b/gi);
        var apos = email.indexOf("@");
        dotpos = email.lastIndexOf(".");
        lastpos = email.length - 1;
        var badEmail = (apos < 1 || dotpos - apos < 2 || lastpos - dotpos < 2);
        if (email == "" || !goodEmail || badEmail)
        {
            //Email address fails
            $('myResponse').style.display = 'inline';
            $('myResponse').style.color = 'red';
            alert('Please enter a valid email');
            $('email').focus();
            return false;
        }
        else
        {
            email = encodeURIComponent(email);
            //Email address succeeds
            $.ajax({
                url: "signup.php?email=" + email,
                success: function(result) {
                    alert('here');
                    $('#myResponse').show();
                    $("loading").show();
                    return false;
                }
            });
        }
    }
    function showResponse(req) {
        $("loading").hide();
        $("myResponse").innerHTML = req.responseText;
        $("myResponse").style.display = "inline";
        $("myResponse").style.color = "blue";
        $("submit").show();
        $("email").invoke('clear');
    }
    function showException(req) {
        $("myResponse").innerHTML = req.responseText;
        alert("An error occured while talking to the server. Please try again.");
        $("loading", "myResponse").invoke('hide');
        $("submit").show();
        $("email").invoke('clear');
    }
</script>

调用这一切的形式如下:

<form method="post" name="subform" id="subform" action="">
                    <input type="text" id="email" name="email" value="">
                    <input type="submit" id="nlsubmit" name="submit" value="Sign up">
                    <div id="myResponse" style="display:none;"></div>
                    <div id="loading" style="display:none;"><img src="/images/wait.gif" alt=""></div>

                </form>

就像我说的那样,时事通讯表更新得很好,尽管如果用户已经存在,如果电子邮件无效等,我需要在同一页面上通知用户。

4

1 回答 1

1

在您的功能中:

        $.ajax({
            url: "signup.php?email=" + email,
            success: function(result) {
                alert('here');
                $('#myResponse').show();
                $("loading").show();
                return false;
            }
        });

'result' 指的是在 signup.php 上回显的任何内容,因此如果 result=="You are subscribed." 这意味着该电子邮件地址已存在于数据库中,否则如果 result=="Thank you for subcribing" 则该电子邮件地址是数据库中的新地址并且新用户已订阅。所以函数应该是这样的:

       $.ajax({
            url: "signup.php?email=" + email,
            success: function(result) {
                if(result=="You are subscribed.")
                {
                  // notify user that email address already exists
                }

                alert('here');
                $('#myResponse').show();
                $("loading").show();
                return false;
            }
        });
于 2013-09-18T14:56:26.727 回答