AJax + PHP + MYSQL通讯订阅者

时间:2013-09-18 14:41:45

标签: javascript php mysql ajax

目前我可以在我的简报表中添加一个新的电子邮件地址,但是我正在努力查询查询的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>

就像我说新闻稿表格更新很好,但我需要用户在同一页面上通知如果它们已经存在,如果电子邮件无效等等。

1 个答案:

答案 0 :(得分:1)

在你的功能中:

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

'result'是指在signup.php上回显的内容,因此如果result ==“您已订阅。”这意味着电子邮件地址已经存在于数据库中,否则如果result ==“感谢您订阅”,则电子邮件地址对数据库来说是新的,并且新用户已订阅。所以函数看起来应该是这样的:

       $.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;
            }
        });