联系表格有效,但"已发送消息"或者"失败"没有显示

时间:2015-02-13 22:11:16

标签: php jquery html contact-form

我的联系表格工作正常,它发送电子邮件到我的服务器和所有,但在页面中它不显示“消息已发送”或“错误”!也许是php文件中的东西?我在这里张贴所有这些都提前谢谢!我的网站是www.nausea.ws

编辑:我使它工作,但它只显示“消息未发送,请再试一次”,但它发生了消息已发送!但“成功”并没有表现出来。这意味着即使邮件已成功发送,也会显示错误消息。

(function($){
    $(document).ready(function() {
        $('#submit-form').click(function(e){

            e.preventDefault();
            var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
            var name  = $('#form_name').val(),
            email  = $('#form_email').val(),
            subject  = $('#form_subject').val(),
            message  = $('#form_message').val(),
            data_html,
            success = $('#success');

            if(name == "")
                $('#form_name').val('Please enter your name.');

            if(subject == "")
                $('#form_subject').val('Please enter your name.');

            if(email == ""){
                $('#form_email').val('Your email is required.');
            }else if(reg.test(email) == false){
                $('#form_email').val('Invalid Email Address.');
            }

            if(message == "")
                $('#form_message').val('Message is required.');

            if(message != "" && name != "" && reg.test(email) != false) {
                data_html = "name=" + name + "&email="+ email + "&message=" + message + "&subject="+ subject;

                //alert(data_html);
                $.ajax({
                    type: 'POST',
                    url: 'contact_form.php',
                    data: data_html,
                    success: function(msg){

                        if (msg == 'sent'){
                            success.html('<div class="alert alert-success">Message <strong>successfully</strong> sent!</div>')  ;
                            $('#form_name').val('');
                            $('#form_email').val('');
                            $('#form_message').val('');
                        }else{
                            success.html('<div class="alert alert-error">Message <strong>not</strong> sent! Please Try Again!</div>')  ; 
                        }
                    }
                });

            }
            return false;
        });
    });
})(jQuery);
textarea {
	height:167px;
	padding:20px;
	margin-bottom:20px; 
	overflow:auto;
}
	
.formSecWrap {
	float: left;
	margin-right: 20px;
}
.formSecWrap2 {
	margin-right: 0px;
}
input[type="text"], input[type="password"], input[type="email"], textarea, select {
    background: #222222;
    border: none;
    border-radius: 3px;
    color: #fff;
    display: block;
    margin: 0 0 10px 0;
    outline: medium none;
    padding: 6px 4px;
    width: 370px;
	font-family:'Oswald', sans-serif;
	font-size: 16px;
	font-weight: 400;
}
input[type="text"]:focus, input[type="password"]:focus, input[type="email"]:focus, textarea:focus {
    box-shadow: 0 0 7px #fff;
}

.formWrap label{
	margin: 0 0 2px 0;
}
.formWrap input[type="submit"]{
	margin: 10px 0 20px 0;
	float: right;
}

a.button,
button,
input[type="submit"],
input[type="reset"],
input[type="button"] {
	background: #7b133c; 
	border: none;
	padding: 4px 12px;
	border-radius: 3px;
	color: #fff;
	display: inline-block;
	font-size: 13px;
	text-decoration: none;
	cursor: pointer;
	margin: 0 5px 10px 0;
	line-height: 21px;
}
a.button:hover,
button:hover,
input[type="submit"]:hover,
input[type="reset"]:hover,
input[type="button"]:hover {
	color: #222;
	background: #ddd;  
}

.alert {
	padding: 8px 35px 8px 14px;
	margin-bottom: 10px;
	margin-top: 10px;
	clear: left;
	color: #c09853;
	text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
	background-color: #fcf8e3;
	border: 1px solid #fbeed5;
	border-radius: 3px;
}
.alert h4 {
	margin: 0;
}
.alert p {
	margin: 0;
}
.alert .close {
	position: relative;
	top: -2px;
	right: -35px;
	line-height: 20px;
}
.success {
	color: #468847;
	background-color: #dff0d8;
	border-color: #d6e9c6;
}
.alert-danger,
.error {
	color: #b94a48;
	background-color: #f2dede;
	border-color: #eed3d7;
}
.alert-info {
	color: #3a87ad;
	background-color: #d9edf7;
	border-color: #bce8f1;
}
button.close {
    background: none;
    border: 0 none;
    cursor: pointer;
    padding: 0;
}
.close {
    color: #000000;
    float: right;
    font-size: 20px;
    font-weight: bold;
    line-height: 20px;
    opacity: 0.2;
    text-shadow: 0 1px 0 #FFFFFF;
}
<div id="contact_form">
    <div class="two-thirds column marginTop formWrap">
        <form action="#" method="post" class="contactForm">
            <div class="formSecWrap">
                <label for="form_name">name</label>
                    <input type="text" id="form_name" name="form_name" value="" />
   
                <label for="form_email">email</label>
                    <input type="text" name="form_email" id="form_email" value="" />
          
                <label for="form_subject">subject</label>
                    <input type="text" name="form_subject" id="form_subject" value="" />
            </div>
            <div class="formSecWrap formSecWrap2">
                <label for="form_message">message</label>
                    <textarea class="textarea" name="form_message" id="form_message"></textarea>
            
                    <input class="button" id="submit-form" type="submit" name="submit" value="Send Message" />
            </div>
            <div id="success"></div>
        </form>
    </div>
</div>

PHP:

if($to) {
$name = $_POST['name'];
$email = $_POST['email'];

$fields = array(
    0 => array(
        'text' => 'Name',
        'val' => $_POST['name']
    ),
    1 => array(
        'text' => 'Email address',
        'val' => $_POST['email']
    ),
    2 => array(
        'text' => 'Message',
        'val' => $_POST['message']
    )
);

$message = "";

foreach($fields as $field) {
    $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}

$headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=utf-8\r\n";
$headers .= "From: \"" . $name . "\" \r\n";
$headers .= "Reply-To: " .  $email . "\r\n";
$message = utf8_decode($message);

mail($to, $subject, $message, $headers);

if ($message){
echo 'sent';
}else{
echo 'failed';
}
}

2 个答案:

答案 0 :(得分:0)

好的,这不是最好的解决方案,但在contact_form.js中我把它放在了:

&#13;
&#13;
 //alert(data_html);
                $.ajax({
                    type: 'POST',
                    url: 'contact_form.php',
                    data: data_html,
                    success: function(msg){
						
						if (message == 'sent'){
                        	success.html('<div class="alert alert-success">Message <strong>successfully</strong> sent!</div>')  ;
                            $('#form_name').val('');
							$('#form_email').val('');
							$('#form_message').val('');
						}if(message == 'failed'){
                            success.html('<div class="alert alert-error">Message <strong>not</strong> sent! Please Try Again!</div>')  ; 
						 }else{
                            success.html('<div class="alert alert-success">Message <strong>successfully</strong> sent!</div>')  ; 
                        
                        }
                    }
                });
    
            }
            return false;
        });
&#13;
&#13;
&#13;

答案 1 :(得分:0)

该动作是否真的写为#..如果是,则删除它: (因为我的代码中没有再发现错误。

<form action="" method="post" class="contactForm">