提交联系表单并使用validate.js

时间:2011-12-15 20:10:02

标签: php javascript jquery html css

这是getting validate.js to workHow to make my contact form send me Email

的推导

我正在整理一个与validate.js和php的简单联系表单。我已经完成了验证,并开始使用php调用服务器的功能 - 但是现在而不是提交它的表单只生成了JS验证确认。我如何允许它提交?结合验证确认并正确提交它会很酷(例如'很棒,你填写表格并通过JS设置提交验证)但是提交并重定向到确认页面也没关系。以下是代码:

美国东部时间12月15日下午4:17更新 正在召集的图书馆:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>

JS被内联调用:

  <script type="text/javascript">
    SubmittingForm=function() {
          document.myform.submit();

}

    $(document).ready(function() {
        $("#fvujq-form1").validate({
            submitHandler:function(form) {
                SubmittingForm();
            },
            rules: {
                name: "required",       // simple rule, converted to {required:true}
                email: {                // compound rule
                    required: true,
                    email: true
                },
                url: {
                    url: true
                },
                comment: {
                    required: true
                }
            },
            messages: {
                comment: "Enter your damn comment."
            }
        });
    });

    jQuery.validator.addMethod(
        "selectNone",
        function(value, element) {
            if (element.value == "none")
            {
                return false;
            }
            else return true;
        },
        "Please select an option."
    );

    $(document).ready(function() {
        $("#fvujq-form2").validate({
            submitHandler:function(form) {
                SubmittingForm();
            },
            rules: {
                sport: {
                    selectNone: true
                }
            }
        });
    });
</script>

表单标记:

 <p style="font-style:italic; font-size:12px; font-weigh: normal; margin-top: -89px;     margin-left: 33px;">Contact me written in a different language.</p> <img src="http://www.cameroncashwell.com/imgs/pointing-left.png" style="float: right; margin-right: 140px; margin-top: -89px;">

<div class="form-div"> 
    <form id="fvujq-form1" style="font-size:22px; color:#333;" method="post" action="send_form_email.php">
      <div class="form-row"><span class="label">Name *</span><input type="text" name="name" /></div>
      <div class="form-row"><span class="label">Email *</span><input type="text" name="email" /></div>
      <div class="form-row"><span class="label">URL</span><input type="text" name="url" /></div>
      <div class="form-row"><span class="label">Comment *</span><textarea name="comment"></textarea></div>
      <div class="form-row"><input class="submit" type="submit" value="Submit"></div>
    </form>
</div>

在'send_form_email.php'中:

<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "chaseoutt@gmail.com";
    $email_subject = "How to be a cool";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['url']) ||
        !isset($_POST['project']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }

    $name = $_POST['name']; // required
    $email = $_POST['email']; // required
    $url = $_POST['url']; // required
    $project = $_POST['project']; // not required
    $comments = $_POST['comments']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "URL: ".clean_string($url)."\n";
    $email_message .= "Project: ".clean_string($project)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

现在,当我正确填写表单时,我从php文件(action =“send_form_email.php”)中获取了这些错误,声明了这些错误:

We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.

We are sorry, but there appears to be a problem with the form you submitted.

Please go back and fix these errors.

1 个答案:

答案 0 :(得分:1)

在你的php检查中你有!isset($_POST['comments'])),但在你的html中你有<textarea name="comment">你需要使它们相同,<textarea name="comments">改变它,你应该全部设置,只要你的正确地填写所有领域。