doujiexi1824 2014-01-09 17:02
浏览 28

为什么输入传递jquery.validate但不传递ajax / php?

I have been trying to submit form data to a database and am trying to use ajax and php.

$(document).ready(function(){
    $('#register input').hover(function()
                               {
                                   $(this).popover('show')
                               });
    $("#register").validate({
        rules:{
            email:{
                required:true,
                email: true
            },
            inquiry:"required"
        },
        messages:{
            email:{
                required:"Please enter your E-Mail Address",
                email:"Please enter a valid E-Mail Address"
            },
            inquiry:"Select a reason for contacting us"
        },
        errorClass: "help-inline",
        errorElement: "span",
        highlight:function(element, errorClass, validClass) {
            $(element).parents('.control-group').addClass('error');
        },
        unhighlight: function(element, errorClass, validClass) {
            $(element).parents('.control-group').removeClass('error');
            $(element).parents('.control-group').addClass('success');
        },
        submitHandler: function register() {
            var status = $('#status');
            var submitbtn = $('#submitbtn');
            var data = {
                email: $('#email').val(),
                inquiry: $('#inquiry').val()
            };

            if (data.email == '' || data.inquiry == '') {
                status.text('Fill out all of the form data');
                return;
            } 

            submitbtn.hide();
            status.text('please wait...');

            $.ajax({
                type: 'post',
                url: 'register.php',
                data: data,
                success: function(responseText) {
                    if (responseText != 'register_success') {
                        status.text(responseText);
                        submitbtn.show();
                        return;
                    }

                    window.scrollTo(0, 0);
                    $('#register').html('OK '+ data.email +', check your email at <u>'+ data.email +'</u> to complete the registration process.');

                    // ajax done
                    // do whatever & close the modal
                }
            });
            return false; // required to block normal submit since you used ajax
        }
    });
});

And here is the php:

<?php
if(!empty($_POST['email']) && isset($_POST['email']) &&  !empty($_POST['inquiry']) &&  isset($_POST['inquiry']) )
    {
        // CONNECT TO THE DATABASE
        include_once("db.php");
        // GATHER THE POSTED DATA INTO LOCAL VARIABLES
        $email = mysqli_real_escape_string($db, $_POST['email']);
        $inquiry = preg_replace('#[^a-z ]#i', '', $_POST['inquiry']);
        // GET USER IP ADDRESS
        $ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
        // DUPLICATE DATA CHECKS FOR EMAIL
        $sql = "SELECT id FROM register WHERE email='$email' LIMIT 1";
        $query = mysqli_query($db, $sql);
        $email_check = mysqli_num_rows($query);
        // FORM DATA ERROR HANDLING
        if($email == "" || $inquiry == ""){
            echo "Please fill in all the fields.";
            exit();
        } else if ($e_check > 0){ 
            echo "That E-Mail Address is already in use";
            exit();
        } else {
            // END FORM DATA ERROR HANDLING
            // Begin Insertion of data into the database
            // Add user info into the database table for the main site table
            $sql = "INSERT INTO register (email, inquiry, ip)
           VALUES('$email','$inquiry','$ip')";
            $query = mysqli_query($db, $sql);
            $uid = mysqli_insert_id($db);
            // Create directory(folder) to hold each user's files(pics, MP3s, etc.)
            if (!file_exists("user/$u")) {
                mkdir("user/$u", 0755);
            }
            // Email the user their activation link
            $to = "$email";
            $from = "auto_responder@MyWebSite.com";
            $subject = 'MyWebSite E-Mail Account Activation';
            $message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>MyWebSite Message</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px; background:#333; font-size:24px; color:#CCC;"><a href="http://www.MyWebSite.com"><img src="http://www.MyWebSite.com/images/MyWebSiteEmailLogo.svg" width="36" height="30" alt="MyWebSite" style="border:none; float:left;"></a>E-Mail Activation</div><div style="padding:24px; font-size:17px;">Hello '.$email.',<br /><br />  <br /><br /><a href="http://www.MyWebSite.com/_/php_includes/activation.php?id='.$uid.'&u='.$u.'&email='.$email.'">Click here to activate your E-Mail</a><br /><br /><br /></b></div></body></html>';
            $headers = "From: $from
";
            $headers .= "MIME-Version: 1.0
";
            $headers .= "Content-type: text/html; charset=iso-8859-1
";
            if(mail($to, $subject, $message, $headers)) {

            } else {

            }
            echo "register_success";
            exit();
        }
        exit();
    }
?>

But when I submit I get a php form error.

<"Please fill in all the fields">

What am I doing wrong

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)