联系表格不起作用

时间:2015-02-12 16:53:33

标签: javascript php jquery contact-form

我有这个网站www.nausea.ws并且在您提交表单时没有任何反应,它只会删除“名称字段”!我还需要在同一页面中的"your message was sent",在表单下方显示!在浏览器控制台上它说:

Uncaught ReferenceError: subject is not definedcontact_form.js:17 (anonymous function)jquery-1.8.3.min.js:2 v.event.dispatchjquery-1.8.3.min.js:2 o.handle.u

这是contact_form.js和contact_form.php:

(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);

PHP:

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to = 'contato@nausea.ws';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
        window.location = 'index.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send an email to gordon@template-help.com');
        window.location = 'index.html';
    </script>
<?php
}
?>

1 个答案:

答案 0 :(得分:1)

我开车进入你的实际源代码(你指向的页面,你有错别字 你有这个变量

  var Nome  = $('#form_name').val(),
                Email  = $('#form_email').val(),
                Assunto  = $('#form_subject').val(),
                Mensagem  = $('#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.');

您应该使用Nome代替nameAssunto代替subject,依此类推。