由于未知的验证错误,PHP表单未捕获消息

时间:2015-08-21 20:40:46

标签: php html forms

由于意外错误,我的表单没有发送消息,我无法看到该错误是什么。它只是显示在一个红色框中,表单没有任何反应。我已多次查看代码,起初它正在寻找错误目录中的php表单。但现在我被卡住了。

网站:http://proverbs.io/

这是我的代码

联系部分包含在内。

的index.php

<?php include 'includes/contact.php';?>

contact.php

<section class="cta text-center">

  <div class="container">

    <div class="row">
      <div class="col-lg-12">
        <h2>Have a suggestion? <span class="highlight">We appreciate feedback!</span></h2>
        <p><button class="btn contact-button" type="button" role="button">Contact us</button></p>
        <div class='contact-form'>

          <form action="php/mail.php" method="POST" role="form">>
            <div class='name'>
              <input class='form-control first' placeholder='First Name' type='text' autofocus
              data-validation-type="string" id="form-name" placeholder="Full Name" name="name">
              <input class='last' placeholder='Last Name' type='text' name="last_name">
            </div>
            <div class='contact'>
              <input class='form-control email' placeholder='E-mail Address' type='text' id="form-email" name="email">
            </div>
            <div class='message'>
              <textarea class="form-control" placeholder='Your message' id="form-message" name="message" maxlength="999" style="resize:none"></textarea>
            </div>
            <div id="success"></div> <!-- For success/fail messages -->
            <footer class="submit-button">
              <button type="submit">Submit</button>
            </footer>
          </form>

        </div>
      </div>
    </div>

  </div>

</section>   

mail.php

<?php

    $email_to = "juan@proverbs.io";
    $email_from = "do-not-reply@proverbs.io";
    $email_subject = "Contact Form submitted";

    if(isset($_POST['email']))
    {

        function return_error($error)
        {
            echo $error;
            die();
        }

        // check for empty required fields
        if (!isset($_POST['name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['message']))
        {
            return_error('incomplete');
        }

        // form field values
        $name = $_POST['name']; // required
        $email = $_POST['email']; // required
        $message = $_POST['message']; // required

        // form validation
        $error_message = "Something went wrong. Just email juan@proverbs.io";

        // name
        $name_exp = "/^[a-z0-9 .\-]+$/i";
        if (!preg_match($name_exp,$name))
        {
            $this_error = 'Please enter a valid name.';
            $error_message .= ($error_message == "") ? $this_error : "<br/>".$this_error;
        }        

        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
        if (!preg_match($email_exp,$email))
        {
            $this_error = 'Please enter a valid email address.';
            $error_message .= ($error_message == "") ? $this_error : "<br/>".$this_error;
        } 

        // if there are validation errors
        if(strlen($error_message) > 0)
        {
            return_error($error_message);
        }

        // prepare email 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 .= "Last Name: ".clean_string($last_name)."\n";
        $email_message .= "Email: ".clean_string($email)."\n";
        $email_message .= "Message: ".clean_string($message)."\n";

        // create email headers
        $headers = 'From: '.$email_from."\r\n".
        'Reply-To: '.$email."\r\n" .
        'X-Mailer: PHP/' . phpversion();
        if (mail($email_to, $email_subject, $email_message, $headers))
        {
            echo 'success';
        }
        else 
        {
            echo 'error';
            die();        
        }
    }
    else
    {
        echo 'incomplete';
        die();
    }
    ?>

1 个答案:

答案 0 :(得分:2)

当我填写表单并点击提交时会发生这种情况。

Erros from console