电子邮件表单验证

时间:2014-11-13 14:50:18

标签: php jquery

我无法验证我的电子邮件表单。最初我使用PHP发送电子邮件,一旦填写表格并使用以下方式提交:

<?php
if(isset($_POST['submit'])){
$to = "me@gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['customer_name'];
$last_name = $_POST['company_name'];
$vat_number = $_POST['vat_number'];
$business_type = $_POST['business_type'];
$subject = "Customer Sign Up";
$message = $first_name . " from " . $last_name . " has applied to be a customer." . "\n\n" . "About their business:" . " " . $_POST['message'] . "\n\n" . "VAT Number: " . $vat_number . "\n\n" . "Business type: " . $_POST['business_type'];

$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);

}
?>

这很有效(source

然后我想在发送之前验证表单,jQuery似乎是一个很好的解决方案,所以我发现this from jQuery.com在发送之前验证表单而不重定向用户。

我复制了这个例子只是为了测试,jQuery与验证一起使用,但是如果正确的话,它之后就不再提交PHP代码了。

<form action="" method="post" class="reg-form">
          <h2>Sign up form</h2>
          <table class="form">
            <tr>
              <td><label for="customer_name">Your Name: </label></td>
              <td><input type="text" name="customer_name" class="text-input" id="test"></td>
            </tr>
            <tr>
              <td><label for="company_name">Company Name: </label></td>
              <td><input type="text" name="company_name" class="text-input"></td>
            </tr>
            <tr>
              <td><label for="vat_number">VAT Number/Tax Code: </label></td>
              <td><input type="text" name="vat_number" class="text-input"></td>
            </tr>
            <tr>
              <td><label for="email">Email: </label></td>
              <td><input type="text" name="email" class="text-input"></td>
            </tr>
            <tr>
              <td><label for="business_type">Physical or online business? </label></td>
              <td><select name="business_type"><option>Physical Store</option><option>Online Store</option><option>Physical &amp; Online store</option></select></td>
            </tr>
            <tr>
              <td><label for="message">Tell us more about your business: </label></td>
              <td><textarea rows="5" name="message" cols="50" class="textarea-input"></textarea></td>
            </tr>
            <tr>
              <td></td>
              <td><input type="submit" value="Submit" class="button green-btn" id="sign-submit" /></td>
            </tr>
          </table>
          </form>

<script>
      $("form").submit(function(event){
        if( $("#test").val() === "correct" ) {
          $("span").text("validated...").show();
          return;
        }
        $("span").text("Not valid!").show().fadeOut(100000);
        event.preventDefault();
      });
    </script>

任何人都可以帮我解决为什么PHP在验证后不会执行的原因吗?

3 个答案:

答案 0 :(得分:0)

表单中没有名称为“submit”的输入字段...在复制粘贴提交按钮时可能会掉线;)

答案 1 :(得分:0)

我想您在复制和粘贴时可能忘记设置action字段格式:)如果没有,请告诉我们完整的代码。

答案 2 :(得分:0)

不要试图从你的工作中拿走任何东西,而是here is a really nice jQuery form validation plugin

它会让你的生活更轻松。

例如:

**HTML**

<form id="myform" action="my-action" method="post" class="reg-form">
      <h2>Sign up form</h2>
      <table class="form">
        <tr>
          <td><label for="customer_name">Your Name: </label></td>
          <td><input type="text" name="customer_name" class="text-input" id="test"></td>
        </tr>
        <tr>
          <td><label for="company_name">Company Name: </label></td>
          <td><input type="text" name="company_name" class="text-input"></td>
        </tr>
        <tr>
          <td><label for="vat_number">VAT Number/Tax Code: </label></td>
          <td><input type="text" name="vat_number" class="text-input"></td>
        </tr>
        <tr>
          <td><label for="email">Email: </label></td>
          <td><input type="text" name="email" class="text-input"></td>
        </tr>
        <tr>
          <td><label for="business_type">Physical or online business? </label></td>
          <td><select name="business_type"><option>Physical Store</option><option>Online Store</option><option>Physical &amp; Online store</option></select></td>
        </tr>
        <tr>
          <td><label for="message">Tell us more about your business: </label></td>
          <td><textarea rows="5" name="message" cols="50" class="textarea-input"></textarea></td>
        </tr>
        <tr>
          <td></td>
          <td><input type="submit" value="Submit" class="button green-btn" id="sign-submit" /></td>
        </tr>
      </table>
      </form>

这将是您的验证脚本:

<强>的jQuery

var validator;

validator = $('#myform').validate({
'rules': {
    'email': {
        'required': ture,
        'email': true
    }
    'messages': {
        'email': {
            'required': "An email address is required",
                'email': "Please use a valid email address."
        }
    },
        'onkeyup': false,
        'submitHandler': function (form) {
        alert('Submit!');
        // form.submit();
    }
});

您需要在jquery库调用之后包含验证器库:

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>