首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AJAX表单不提交AJAX

AJAX表单不提交AJAX
EN

Stack Overflow用户
提问于 2016-04-25 22:11:55
回答 2查看 739关注 0票数 0

我正在使用AJAX和jQuery编写一个登录表单,但是表单提交的方式不对:它将返回到process.php,而不是在index.php上返回消息。

index.html

代码语言:javascript
复制
<form id="login" action="process.php" method="POST">

    <div id="name-group" class="form-group">
        <label for="name">Name</label>
        <input type="text" class="form-control" name="name" placeholder="JohnDoe">
    </div>

    <div id="email-group" class="form-group">
        <label for="email">Email</label>
        <input type="text" class="form-control" name="email" placeholder="john@doe.doe">
    </div>

    <button type="submit" class="btn btn-success">Submit <span class="fa fa-arrow-right"></span></button>

</form>

在表格上没有发现任何错误。至少我在那里找不到任何错误。

index.html;这是JS脚本

代码语言:javascript
复制
$(document).ready(function() {

    // process the form
    $('#login').submit(function(event) {

        // get the form data
        // there are many ways to get this data using jQuery (you can use the class or id also)
        var formData = {
            'name'              : $('input[name=name]').val(),
            'email'             : $('input[name=email]').val()
        };

$.ajax({
    type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
    url         : 'process.php', // the url where we want to POST
    data        : formData, // our data object
    dataType    : 'json' // what type of data do we expect back from the server
})
    // using the done promise callback
    .done(function(data) {

        // log data to the console so we can see
        console.log(data);

        // here we will handle errors and validation messages
        if ( ! data.success) {

            // handle errors for name ---------------
            if (data.errors.name) {
                $('#name-group').addClass('has-error'); // add the error class to show red input
                $('#name-group').append('<div class="help-block">' + data.errors.name + '</div>'); // add the actual error message under our input
            }

            // handle errors for email ---------------
            if (data.errors.email) {
                $('#email-group').addClass('has-error'); // add the error class to show red input
                $('#email-group').append('<div class="help-block">' + data.errors.email + '</div>'); // add the actual error message under our input
            }

        } else {

            $('form').append('<div class="alert alert-success">' + data.message + '</div>');
            alert('success');

        }

    });

});

process.php

代码语言:javascript
复制
<?php
$errors         = array();      // array to hold validation errors
$data           = array();      // array to pass back data

    if (empty($_POST['name']))
        $errors['name'] = 'Name is required.';

    if (empty($_POST['email']))
        $errors['email'] = 'Email is required.';

    // if there are any errors in our errors array, return a success boolean of false
    if ( ! empty($errors)) {

        // if there are items in our errors array, return those errors
        $data['success'] = false;
        $data['errors']  = $errors;
    } else {

        // if there are no errors process our form, then return a message

        // DO ALL YOUR FORM PROCESSING HERE
        // THIS CAN BE WHATEVER YOU WANT TO DO (LOGIN, SAVE, UPDATE, WHATEVER)

        // show a message of success and provide a true success variable
        $data['success'] = true;
        $data['message'] = 'Success!';
    }

    // return all our data to an AJAX call
    echo json_encode($data);

我快把头发拔出来了,请帮我解决一下出了什么问题。

EN

回答 2

Stack Overflow用户

发布于 2016-04-25 22:15:24

您要提交两次表单,一次单击submit按钮,另一次在AJAX函数上--您需要停止默认提交,这样AJAX表单才能运行:

编辑您的JS,包括以下内容:

代码语言:javascript
复制
 $('#login').submit(function(event) {
    event.preventDefault();
//rest of code
票数 0
EN

Stack Overflow用户

发布于 2016-04-25 22:22:22

在使用表单提交事件时,可以通过以下方式防止表单重定向到另一个页面。

代码语言:javascript
复制
return false 

在提交函数的末尾

就像..。

代码语言:javascript
复制
$('#login').submit(function(event) {
     // your code
    return false;
}

如果仍然不起作用,那么您可能需要尝试一下

代码语言:javascript
复制
$(document).on('submit','#login',function(event){
    // your code
    return false;
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36852018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档