Html5联系表单 - javascript和php验证问题

时间:2013-01-05 21:15:32

标签: php javascript html5

在我开始之前请除了我的appologises,如果这很长,我完全是Html5的新手(通常是在Flash中设计)并且刚开始。我最近下载了一个名为Brownie的免费模板,除了联系表之外,我还了解大多数事情。我认为它使用的是Javascript,PHP和Ajax。我在当地有点工作,但事情并不像我想要的那样。我正在尝试做的是使用错误消息或成功消息e.t.c验证字段。这是我的html表单代码:

<p class="required_info"><span>*</span> Required</p>
<!-- SUCCESS MESSAGE -->
<div class="success_box none"> Email successfully sent. Thank you! </div>
<!-- END SUCCESS MESSAGE -->

<!-- START CONTACT FORM -->
<form action="#" class="contact_form">
  <p>
    <label for="name">Name <span>*</span></label>
    <input type="text" class="inputText" id="name" name="name" placeholder="Please enter your full name" pattern="[a-zA-Z ]+"  onkeypress="return alpha(event,letters)" required minlength="5" maxlength="50" onpaste="return false;" tabindex="1"/>
    <div class="success_title none"> You've entered your name. </div>
    <div class="error_title none">* Please enter your full name. </div>
    </p>
  <div class="clear"></div>
  <p>
    <label for="company">Company </label>
    <input type="text" class="inputText" id="company" name="company" placeholder="Company Name" minlength="3" maxlength="50" onpaste="return false;" tabindex="2"/>
  </p>
  <div class="clear"></div>
  <p>
    <label for="email">E-mail <span>*</span></label>
    <input type="text" class="inputText"  id="email" name="email" placeholder="Please enter a valid email address" required minlength="5" maxlength="50" onpaste="return false;" tabindex="3"/>
  <div class="error_title none">* Please enter a valid email. </div>
  </p>

  <div class="clear"></div>
  <p>
    <label for="message">Message <span>*</span></label>
    <textarea class="inputTextarea" cols="88" rows="6" id="message" name="message" placeholder="Please enter your message" required tabindex="4"></textarea>
    <div class="error_title none">* Please enter your message. </div>
  </p>
  <div class="clear padding20"></div>
  <p class="submit"><input type="reset" class="resetBtn button white" onclick="resetFields()" value="Reset" /> <a href="javascript:void(0);" class="button white" onclick="$('.contact_form').submit();">Send</a> </p>
</form>
<!-- END CONTACT FORM --> 

<!-- ERROR MESSAGE -->
<div class="error_box none"> Please complete all required fields [ highlighted in red ]. </div>
<!-- END ERROR MESSAGE --> 

这是javascript代码:

$(document).ready(function () {

    $('#name').focus(function () {
        $(this).removeClass('error_class');
    });

    $('#email').focus(function () {
        $(this).removeClass('error_class');
    });

    $('#message').focus(function () {
        $(this).removeClass('error_class');
    });

    $('.contact_form').submit(function () {

        hasError = false;

        if ($('#name').val() == '') {
            $('#name').addClass('error_class');
            hasError = true;
        }

        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var emailaddressVal = $('#email').val();
        if (emailaddressVal == '') {
            $('#email').addClass('error_class');
            hasError = true;
        }
        else if (!emailReg.test(emailaddressVal)) {
            $('#email').addClass('error_class');
            hasError = true;
        }

        if ($('#email').val() == '') {
            $('#email').addClass('error_class');
            hasError = true;
        }

        if ($('#message').val() == '') {
            $('#message').addClass('error_class');
            hasError = true;
        }

        if (hasError == true) {
            $('.info_box').hide();
            $('.error_box').show();
            $('.error_title').show();
        }
        else {
            $.ajax({
                type: 'POST',
                url: 'contact.php',
                cache: false,
                data: $(".contact_form").serialize(),
                success: function (data) {
                    if (data == "error") {
                        $('.success_box').hide();
                        $('.success_title').hide();
                        $('.error_box').show();
                        $('.error_title').show();
                    }
                    else {
                        $('.error_box').hide();
                        $('.error_title').hide();
                        $('.success_box').show();
                        $('.success_title').hide();
                        $('.resetBtn').hide();
                        $('.submit').hide();
                    }
                }
            });
        }

        return false;
    });
});

这是PHP代码:

<?php

$your_email = 'name@domain.com'; // Your email address
$subject = 'Email from your contact form'; // Email subject

$name = isset($_POST['name']) && $_POST['name'] ? $_POST['name'] : ''; // Visitor Name
$company = isset($_POST['company']) && $_POST['company'] ? $_POST['company'] : ''; // Visitor Company
$email = isset($_POST['email']) && $_POST['email'] ? $_POST['email'] : ''; // Visitor Email
$message = isset($_POST['message']) && $_POST['message'] ? $_POST['message'] : ''; // Visitor Message
$website = isset($_POST['website']) && $_POST['website'] ? $_POST['website'] : ''; // Visitor Message

$full_message = 'Website: '.$website. "\r\n\r\n Message:".$message;

if($name && $email && $message)
{
    $headers = 'From: '.$name.' <'.$email.'>' . "\r\n" .
    'Reply-To: '.$email.'' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
    $headers .= 'Content-type: text/plain; charset=UTF-8' . "\r\n";

    //------------------------------------------------
    // Send out email to site admin
    //------------------------------------------------
    if(@mail($your_email, $subject, $full_message, $headers))
        die("success");
    else
        die("error");
}
else
{
    die("error");
}

?>

当按下发送按钮时,我希望它检查是否在每个输入字段中输入了任何数据 - 如果是,则添加成功消息,如果没有添加错误消息。我有错误消息工作,但无法获得成功消息在单个输入字段上工作。任何吸烟都会有所帮助......

1 个答案:

答案 0 :(得分:0)

我怀疑是因为你为每个变量分配了true或false,而不是php脚本中变量的值。 Isset($ _ POST [test])为true,而不是测试表值的值。