在我的网站上有一个表单字段的问题

时间:2012-05-01 22:45:05

标签: php jquery ajax forms

我的网站上有一个基本表单,并且一直在尝试添加电话号码字段。

标题验证为:

//process the contact form
    $('#submit-form').click(function(){
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        var names  = $('#contact-form [name="contact-names"]').val();  
        var email_address = $('#contact-form [name="contact-email"]').val();
        var phone = $('#contact-form [name="phone"]').val();
        var comment  = $.trim($('#contact-form #message').val());
        var data_html ='' ;

        if(names == ""){
            $('.name-required').html('Please enter your name.');
        }else{
            $('.name-required').html('');
        }
        if(phone == ""){
            $('.phone-required').html('Please enter your phone number.');
        }else{
            $('.phone-required').html('');
        }
        if(email_address == ""){
            $('.email-required').html('Your email is required.');
        }else if(reg.test(email_address) == false){
            $('.email-required').html('Invalid Email Address.');
        }else{
            $('.email-required').html('');
        }

        if(comment == ""){
            $('.comment-required').html('Comment is required.');
        }else{
            $('.comment-required').html('');
        }

        if(comment != "" && names != "" && reg.test(email_address) != false) {
            data_html = "names="+ names + "&phone" + phone + "&comment=" + comment + "&email_address="+ email_address;
            //alert(data_html);
            $.ajax({
                type: 'POST',
                url: 'php-includes/contact_send.php',
                data: data_html,
                success: function(msg){
                    if (msg == 'sent'){

                        $('#contact-form [name="contact-names"]').val('');  
                        $('#contact-form [name="contact-email"]').val('');
                        $('#contact-form #contact-phone').val('');
                        $('#contact-form #message').val('');
                        $(window.location = "http://theauroraclinic.com/thank-you.html");
                    }else{
                        $('#success').html('<div class="error">Mail Error. Please Try Again!</div>')  ; 
                    }
                }
            });

        }
        return false;
    });

});
</script> 

实际表格是:

<form id="contact-form" method="post" name="contact-form" class="infield rounded-fields form-preset">
<h3>We respect your privacy. </h3>
<h5> Please be assured that we will not share any information without your prior consent.</h5>
<p><span class="note">All fields are required!</span>
<p><label for="name">Name</label>
<input class="text" name="contact-names" type="text" id="name" />
<span class="name-required"></span></p>
<p><label for="email">Email</label>
<input class="text" name="contact-email" type="text" id="email" />
<span class="email-required"></span></p>
<p><label for="phone">Phone</label>
<input class="text" name="contact-phone" type="text" id="phone" />
<span class="phone-required"></span></p>
<p><label for="message" class="message-label">Message</label>
<textarea class="text-area" name="contact-comment" id="message"></textarea>
<span class="comment-required"></span></p>
<p><input class="send" id="submit-form" name="submit" type="submit" value="Send" /></p>                       
</form>

表单处理php文件是:

<?php
 $names = $_POST['names'];
 $email = $_POST['email_address'];
 $phone = $_POST['phone'];
 $comment = $_POST['comment'];
 $to ='name@email.com';

 $message = "";
 $message .= "*Name: " . htmlspecialchars($names, ENT_QUOTES) . "<br>\n";
 $message .= "*Email: " . htmlspecialchars($email, ENT_QUOTES) . "<br>\n";
 $message .= "*Phone: " . htmlspecialchars($phone, ENT_QUOTES) . "<br>\n";
 $message .= "Comment: " . htmlspecialchars($comment, ENT_QUOTES) . "<br>\n";
 $lowmsg = strtolower($message);

 $headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n";
 $headers .= "From: \"" . $names . "\" <" . $email . ">\r\n";
 $headers .= "Reply-To: " .  $email . "\r\n";
 $message = utf8_decode($message);  mail($to, "Note from the Contact Form", $message, $headers);

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

表单仍然正常,但不会将任何输入传递到电话号码字段。我确实在提交时添加了重定向。奇怪的是,这只会影响1场。任何帮助都会很棒,我对js a&amp; PHP!

3 个答案:

答案 0 :(得分:0)

电话字段的名称为contact-phone而不是phone

答案 1 :(得分:0)

在javascript中,[name="phone"]应更改为[name="contact-phone"]

答案 2 :(得分:0)

data_html = "names="+ names + "&phone" + phone + "&comment=" + comment + "&email_address="+ email_address;

更改此内容...&amp; phone =缺失

data_html = "names="+ names + "&phone=" + phone + "&comment=" + comment + "&email_address="+ email_address;