如何在用户提交表单后显示成功消息

时间:2015-07-05 13:12:30

标签: javascript php jquery ajax xmlhttprequest

        $('input[type="submit"]').click(function(){
            resetErrors();
            $.each($('form input, form textarea, form select'), function(i, v){
                if(v.type !== 'submit'){
                    data[v.name] = v.value;
                }
            });
            $.ajax({
                dataType: 'json',
                type: 'POST',
                url: 'libs/contact.php',
                data: data,
                success: function(resp){
                    if(resp === true){
                        $('form').submit();
                        if(status == "success"){
                            console.log('Success')
                        }
                        return false;
                    } else {
                        $.each(resp, function(i, v){
                            console.log(i + " => " + v);
                            var msg = '<label class="error" for"' +i+ '">' +v+ '</label>';
                            $('input[name="' +i+ '"], textarea[name="' +i+ '"], select[name="' +i+ '"]').addClass('inputTextError').after(msg);
                        });
                        var keys = Object.keys(resp);
                        $('input[name="' +keys[0]+ '"]').focus
                    }
                    return false;
                },
                error: function(){
                    console.log('There was a problem checking fields');
                }

            });
            return false;
        });
    });
        function resetErrors(){
            $('form input, form textarea').removeClass('inputTextError');
            $('label.error').remove();
    }

这是我的代码我已经尝试了一切,但仍然没有工作HELP PLZ 我正在服务器上进行所有验证我只是使用ajax来显示错误所以我想显示成功消息但是它有点混乱

这是我的PHP代码     

require_once('phpmailer/PHPMailerAutoload.php');

$errors = [];

if(isset($_POST['name'], $_POST['email'], $_POST['number'], $_POST['message'])){
    $fields = [
        'name' => $_POST['name'],
        'email' => $_POST['email'],
        'number' => $_POST['number'],
        'message' => $_POST['message']
    ];
    if(empty($fields['name'])){
        $errors['name'] = 'Come on you have a name right?';
    }
    if(empty($fields['email'])){
        $errors['email'] = 'Yes right, we need your email so that we contact you';
    }

    if(empty($fields['number'])){
        $errors['number'] = 'We also need your number';
    }

    if(empty($fields['message'])){
        $errors['message'] = 'You have come this far for sure you have something to say right';
    }
    if(count($errors) > 0){
        if ( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ){
            echo json_encode($errors);
            exit;
        }

        echo "<ul>";
        foreach($errors as $key => $value){
            echo '<li>' . $value . '</li>';
        }
        echo '</ul>';
        exit;
    }
    /*foreach($fields as $field => $data){
        if(empty($data)){
            $errors[] = 'The ' . $field . ' field is required.';
        }
    }*/
    if(empty($errors)){
        $m = new PHPMailer;

        $m->isSMTP();
        $m->SMTPAuth = true;

        $m->Host = 'smtp.gmail.com';
        $m->Username = '';
        $m->Password = '';
        $m->SMTPSecure = 'ssl';
        $m->Port = 465;

        $m->isHTML();

        $m->Subject = 'Contact form submitted';
        $m->Body = 'From: ' . $fields['name'] . '(' .$fields['email'] . ')<p>' . $fields['message'] . '</p><p>' . $fields['number'] . '</p>';

        $m->FromName = 'contact';

        $m->AddReplyTo($fields['email'], $fields['name']);

        $m->AddAddress('');

        if($m->send()){
            header('Location: ../contact.php');
            die();
        } else {
            $errors[] = 'Sorry could not send email. Try again later.';
        }
    }
} else {
    echo 'Oops something went wrong';
}

$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;

header('Location: ../contact.php');

1 个答案:

答案 0 :(得分:0)

很难找到代码中的内容,但我想这应该与我所做的更改有关。

jQuery / ajax

success: function(resp){
    if(resp === 'Success'){
        console.log('Success');
        $('form').submit();
        return false;
    } else {
        $.each(resp, function(i, v){
            console.log(i + " => " + v);
            var msg = '<label class="error" for"' +i+ '">' +v+ '</label>';
            $('input[name="' +i+ '"], textarea[name="' +i+ '"], select[name="' +i+ '"]').addClass('inputTextError').after(msg);
        });
        var keys = Object.keys(resp);
        $('input[name="' +keys[0]+ '"]').focus
    }
    return false;
}

PHP

<?php

if(isset($_POST['name'], $_POST['email'], $_POST['number'], $_POST['message'])){
    $errors = array();
    $fields = [
        'name' => $_POST['name'],
        'email' => $_POST['email'],
        'number' => $_POST['number'],
        'message' => $_POST['message']
    ];
    if(empty($fields['name'])){
        $errors['name'] = 'Come on you have a name right?';
    }
    if(empty($fields['email'])){
        $errors['email'] = 'Yes right, we need your email so that we contact you';
    }

    if(empty($fields['number'])){
        $errors['number'] = 'We also need your number';
    }
    if(empty($fields['message'])){
        $errors['message'] = 'You have come this far for sure you have something to say right';
    }
    if(count($errors) > 0){
        if ( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ){
            echo json_encode($errors);
            exit;
        }

        echo "<ul>";
        foreach($errors as $key => $value){
            echo '<li>' . $value . '</li>';
        }
        echo '</ul>';
        exit;
    }
    /*foreach($fields as $field => $data){
        if(empty($data)){
            $errors[] = 'The ' . $field . ' field is required.';
        }
    }*/
    if(empty($errors)){
        $m = new PHPMailer;

        $m->isSMTP();
        $m->SMTPAuth = true;

        $m->Host = 'smtp.gmail.com';
        $m->Username = '';
        $m->Password = '';
        $m->SMTPSecure = 'ssl';
        $m->Port = 465;

        $m->isHTML();

        $m->Subject = 'Contact form submitted';
        $m->Body = 'From: ' . $fields['name'] . '(' .$fields['email'] . ')<p>' . $fields['message'] . '</p><p>' . $fields['number'] . '</p>';

        $m->FromName = 'contact';

        $m->AddReplyTo($fields['email'], $fields['name']);

        $m->AddAddress('hello@jilstudios.com', 'JIL Studios');

        if($m->send()){
            echo 'Success';
        } else {
            $errors[] = 'Sorry could not send email. Try again later.';
        }
    }
} else {
    echo 'Oops something went wrong';
}

$_SESSION['errors'] = $errors; $_SESSION['fields'] = $fields;

header('Location: ../contact.php');

?>