简体   繁体   English

Ajax验证无请求发送到服务器

[英]Ajax validation no request send to server

I apologize for opening what might be a very basic post, I am learning Ajax please keep that in mind. 对于打开一个非常基本的帖子,我深表歉意。我正在学习Ajax,请记住这一点。

I have a simple registration form. 我有一个简单的注册表。

What im trying to do 我想做什么

  • validate the form 验证表格
  • if all is in order register new user 如果都是为了注册新用户

I have managed to get the Ajax script to register a new user but my problem comes in with the validation part hench im turning here for a bit of help and advice 我已经设法获取Ajax脚本来注册新用户,但是我的问题来自验证部分,我在这里转向了一点帮助和建议。

HTML HTML

 <div id="regResponse">
 </div>
 <form class="form-horizontal" id="regForm" role="form" method="post" action="../register.php" >
     <div class="form-group">
         <label class="control-label col-sm-2" for="regName">Name:</label>
         <div class="col-sm-10">
             <input type="text" name="regName" class="form-control" id="name" placeholder="">
         </div>
     </div>
     <div class="form-group">
         <label class="control-label col-sm-2" for="regLastName">Surname:</label>
         <div class="col-sm-10">          
             <input type="text" name="regLastname" class="form-control" id="lastname" placeholder="">
         </div>
     </div>
     <div class="form-group">
         <label class="control-label col-sm-2" for="regEmail">Email:</label>
         <div class="col-sm-10">          
             <input type="text" name="regEmail" class="form-control" id="regEmail" placeholder="">
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-sm-2" for="regPword">Pword:</label>
        <div class="col-sm-10">          
            <input type="text" name="regPword" class="form-control" id="regPword" placeholder="">
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-sm-2" for="confRegPword">Confirm Pword:</label>
        <div class="col-sm-10">          
            <input type="text" name="confRegPword" class="form-control" id="regPword2" placeholder="">
        </div>

JQUERY AJAX JQUERY AJAX

function sendForm() {
    var valid;
    valid = validateContact()
    if(valid){
        // Get the form.
        var form = $('#regForm');
        // Get the messages div.
        var formMessages = $('#regResponse');
        // Set up an event listener for the contact form.
        $(form).submit(function(e) {
            // Stop the browser from submitting the form.
            e.preventDefault();
            // Serialize the form data.
            var formData = $(form).serialize();
            // Submit the form using AJAX.
            $.ajax({
                type: 'POST',
                url: $(form).attr('action'),
                data: formData
            })
            .done(function(response) {
                // Make sure that the formMessages div has the 'success' class.
                $(formMessages).removeClass('error').addClass('success');
                // Set the message text.
                $(formMessages).html(response); // < html();
                // Clear the form.
                $('').val('')
            })
            .fail(function(data) {
                // Make sure that the formMessages div has the 'error' class.
                $(formMessages).removeClass('success').addClass('error');
                // Set the message text.
                var messageHtml = data.responseText !== '' ? data.responseText : 'Oops! An error occured and your message could not be sent.';
                $(formMessages).html(messageHtml); // < html()
            });
        });
    }
}

function validateContact(){
    var valid = true;
    var name = document.getElementById("name").value;
    var lastname = document.getElementById("lastname").value;
    var email = document.getElementById("regEmail").value;  
    if(name ==''){
        document.getElementById("name").style.borderColor="red";
        name.value="Please Enter Name";
        valid = false;  
    }
    if(lastname ==''){
        valid = false;  
    }
    if(email ==''){
        valid = false;  
    }
    return valid;
}

PHP PHP

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    //get variables from reg form
    $name = $_POST['regName'];
    $lastName = $_POST['regLastname'];
    $email = $_POST['regEmail'];
    :
    $sql ="INSERT INTO members......."
    ($result = mysql_query($sql) or trigger_error(mysql_error()."in".$sql);
    if($result){
        echo '<h3 style="blue">Registration Succesesfull</h3>'; 
    }
    else{
        echo '<h3 style="blue">OOPS...Something went wrong here</h3>';  
    }   
}//request POST method

Like I said as form the registration part all is working but as soon as I added the JavaScript validation the whole script stopped working. 就像我说的那样,注册部分都在工作,但是一旦我添加了JavaScript验证,整个脚本就停止了工作。 My biggest problem is that my browser is not showing me any errors so I dont know where I am going wrong 我最大的问题是我的浏览器没有显示任何错误,所以我不知道我要去哪里

Any help will be much appreciated 任何帮助都感激不尽

Your sendForm function is not triggered. 您的sendForm函数未触发。 Code below as your reference, is the right way to trigger submit event via jquery. 以下代码供您参考,是通过jquery触发Submit事件的正确方法。

jQuery jQuery的

$(function() {
    $('form').submit(function(e) {
        e.preventDefault();
        var valid;
        valid = validateContact()
        if(valid ) {
            $.ajax({
                type: 'POST',
                url: "http://facebook.com",
                data: {},
                dataType: 'json',
                success: function() {
                    alert('success');
                },
                error: function() {
                    alert('error');
                }
            })
        }
    });
});


function validateContact(){
    var valid = true;
    var name = document.getElementById("name").value;
    var lastname = document.getElementById("lastname").value;
    var email = document.getElementById("regEmail").value;  
    if(name ==''){
        document.getElementById("name").style.borderColor="red";
        name.value="Please Enter Name";
    valid = false;  
    }
    if(lastname ==''){
    valid = false;  
    }
    if(email ==''){
    valid = false;  
    }
    return valid;
}

我认为您需要在html中添加一个按钮,并对该按钮的click事件调用函数sendForm()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM