繁体   English   中英

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

[英]Submitting a Contact form and using validate.js

这是使validate.js正常工作以及如何使我的联系表发送给我的电子邮件的派生

我将使用validate.js和php编写一个简单的联系表。 我已经完成验证,然后开始使用php-BUT调用服务器的功能,现在提交表单的方式不是生成JS验证确认,而是通过php-BUT进行。 如何允许它提交? 结合验证确认并正确提交是很酷的(例如,“太好了,您填写了表格,并使用Validate通过JS安装程序提交了)。但是也可以提交并重定向到确认页面。 下面是代码:

EST的12/15 4:17 PM EST库被更新:

<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.

在您的php检查中,您具有!isset($_POST['comments']))但是在您的html中,您具有<textarea name="comment">您需要使它们相同, <textarea name="comments">只要您正确填写所有字段,就应该设置好所有条件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM