Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php jQuery AJAX错误处理问题_Php_Jquery_Html_Twitter Bootstrap 3 - Fatal编程技术网

Php jQuery AJAX错误处理问题

Php jQuery AJAX错误处理问题,php,jquery,html,twitter-bootstrap-3,Php,Jquery,Html,Twitter Bootstrap 3,我正在制作一个表单,在成功时将显示一个带有成功或失败文本的引导模式。我面临的问题是,我的成功部分正在工作模式显示成功消息,但失败部分显示模式但为空,它缺少错误文本。我似乎没有发现问题,也许是因为我已经看了很久了。任何帮助都将不胜感激 代码如下: HTML: 改变 $(表单).提交(函数(事件){ 到 $(表单)。查找('[type=“submit”]')。单击(函数(事件){ 你会得到 // Oops! There was a problem with your submission. Plea

我正在制作一个表单,在成功时将显示一个带有成功或失败文本的引导模式。我面临的问题是,我的成功部分正在工作模式显示成功消息,但失败部分显示模式但为空,它缺少错误文本。我似乎没有发现问题,也许是因为我已经看了很久了。任何帮助都将不胜感激

代码如下: HTML:

改变

$(表单).提交(函数(事件){

$(表单)。查找('[type=“submit”]')。单击(函数(事件){

你会得到

// Oops! There was a problem with your submission. Please complete the form and try again.

问题是您正在使用表单标签,HTML5不允许在填写必填字段之前提交表单,因此您的表单没有首先提交。

没有错误显示,它全部为空。甚至没有显示警报。当我进行测试时,我省略了一个输入字段,以便php将给出http_响应_代码(400)如果其中任何一个为空。在这种情况下,我如何调用插入mail.php文件中的失败文本?当我离开一个输入字段empy时,网络选项卡中没有任何活动,只有当我完成所有输入字段时,我才能在网络选项卡中获得成功活动。链接到网络选项卡pic:这是我试图告诉你的,如果我离开,则不会发出任何请求i’只有在我填写完后,输入字段才是空的:)运气不好,什么也不会发生:(试试上面更新的代码,我在现场试用过了&效果很好,谢谢,节省了我很多时间;)我喝啤酒:)
<?php

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $name = strip_tags(trim($_POST["name"]));
    $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $subject = strip_tags(trim($_POST["subject"]));
    $subject = str_replace(array("\r","\n"),array(" "," "),$subject);
    $select = trim($_POST["select"]);
    $message = trim($_POST["message"]);

    // Check that data was sent to the mailer.
    if (empty($name) OR empty($message) OR empty($subject) OR empty($select) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Oops! There was a problem with your submission. Please complete the form and try again.";
        exit;
    }

    // Set the recipient email address.
    // FIXME: Update this to your desired email address.
    $recipient = "info@email.com";

    // Build the email content.
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n";
    $email_content .= "Category: $select\n";
    $email_content .= "Subject: $subject\n\n";
    $email_content .= "Message: $message\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        http_response_code(200);
        echo "Thank You! Your message has been sent.";
    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Oops! Something went wrong and we couldn't send your message.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";

}
$(function() {
// Get the form.
var form = $('#ajax-contact');

// Get the messages div.
var formMessages = $('#form-messages');

// TODO: The rest of the code will go here...
// Set up an event listener for the contact form.
$(form).submit(function(event) {
    // Stop the browser from submitting the form.
    event.preventDefault();

    // Serialize the form data.
    var formData = $(form).serialize();

    // Submit the form using AJAX.
    $.ajax({
        type: 'POST',
        url: $(form).attr('action'),
        data: formData,
        success: function(response) {
            // Make sure that the formMessages div has the 'success' class.
            $(formMessages).removeClass('error');
            $(formMessages).addClass('success');
            console.log(response);
            // Set the message text.
            $(formMessages).text(response);

            // Clear the form.
            $('#name').val('');
            $('#email').val('');
            $('#select').val('');
            $('#subject').val('');
            $('#message').val('');
        },

        error: function(textStatus) {
            alert("asd");
            // Make sure that the formMessages div has the 'error' class.
            $(formMessages).removeClass('success');
            $(formMessages).addClass('error');
            console.log(textStatus);
            // Set the message text.
            if (textStatus !== '') {
                $(formMessages).text(textStatus);
            } else {
                $(formMessages).text('Oops! An error occured and your message could not be sent.');
            }
        }
    });
});
});
// Oops! There was a problem with your submission. Please complete the form and try again.