douwen5584 2014-04-17 03:30
浏览 47
已采纳

使用php提交表单,通过javascript进行电子邮件检查

First time poster, long time reader, so I'll get right to the point.

I'm working on a project for school and this question kind of goes way beyond the requirements of the project, but it'll look awesome once it's finished. I've got 3 pieces of code that aren't cooperating--a piece of html/php, a piece of php, and javascript.

The ultimate goal of my code is this: Here's a form, submit your email and IF it's a @trnty.edu address (my school), submit the form. The problem currently is that the form submits blank data--proof being the many empty lines on my sql server.

I've tested manually setting variables, and it does work (via the emailsubmit.php code), the emailcheck.js code does check for a proper email, but they don't talk to each other properly.

Ya'll mind giving me a hand? I've been at this for about 3 weeks searching this (and other) websites via Google for possible solutions. Many thanks! (my form code from the homepage)

    <div id="signupform">
    <form id="signup" action="scripts/emailsubmit.php" method="POST">
        <input type="email" name="email" placeholder="school email address" />
            <button id="sub">Submit</button>
    </form>

My current Javascript -- I'm not sure what or how to fill in the blank...

$(function(){
$('#signup').submit(function() 
    {
        if(validateEmail($('input').val()))
            {
                return true;
            }
        else
        {
                return false;
        }
});

    function validateEmail(email)
    {
    var re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
    if (re.test(email)) 
        {
            if (email.indexOf('@trnty.edu', email.length - '@trnty.edu'.length) !== -1) 
                {
                    //alert('Submission was successful.'); //if true, submit form -- see video
                    return true;
                }
                 else 
                    {
                        alert('Email must be a Trinity email address (your.name@trnty.edu).');
                       return false;
                    }
        } 
        else {alert('Not a valid e-mail address.');}
    }


    });

Myphp code.

    <?php
    $dbhost = 'localhost';
    $dbuser = 'service';
    $dbpass = '!@#$%';
    $db = 'tbv_main';
    $con = mysqli_connect($dbhost,$dbuser,$dbpass,$db);

    //$email = $_POST['email'];
    //$email = 'itworked@kickass.net';
    $sql = "INSERT INTO stage1 (email, counter) VALUES ('$email', NULL)";

    if (!mysqli_query($con,$sql))
        {
            die('Error: ' . mysqli_error($con));
        }
    echo "Email: $email , 1 record added";
    ?>
  • 写回答

1条回答 默认 最新

  • doulu1867 2014-04-17 03:37
    关注

    your jQuery .submit() is cancelling the default form submission with return false; but there's no ajax to send the data to the server, so what you actually want to do is return false after the negative alerts, and return true at when your regexp passes, then check it in your submit function.

    $('#signup').submit(function() {
        if(validateEmail($('input').val())){
            return true;
        }else{
            return false;
        }
    });
    

    Then in your validate function.

    var re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
    if (re.test(email)) {
        if (email.indexOf('@trnty.edu', email.length - '@trnty.edu'.length) !== -1) 
            {
                //alert('Submission was successful.'); //if true, submit form -- see video
                return true;
            }
         else {
            alert('Email must be a Trinity email address (your.name@trnty.edu).');
            return false;
        }
    } else {
        alert('Not a valid e-mail address.');
        return false;
    }
    return false;
    

    In doing so, your form will submit when the regexp is properly validated and the page will refresh causing your php code to fire.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改