简体   繁体   English

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

[英]How can I display a success message after the user submit the form

        $('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();
    }

Here's my code I have tried everything but still ain't working HELP PLZ I'm doing all my validation on the server I'm just using ajax to display the errors so I would like to display the success message too but it's a little a bit confusing 这是我的代码,我已经尝试了所有方法,但仍然无法正常工作HELP PLZ我正在服务器上进行所有验证,我只是使用ajax来显示错误,所以我也想显示成功消息,但这有点有点混乱

Here's my PHP code 这是我的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');

It's hard to find out what is what in your code, but i guess this should work with the changes i've made. 很难找到代码中的内容,但是我想这应该可以与我所做的更改一起使用。

jQuery / ajax jQuery /阿贾克斯

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

<?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');

?>

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

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