js代码实施后,联系表单PHP脚本停止工作

时间:2019-07-15 15:56:36

标签: javascript php jquery html ajax

我已经安装了一个PHP脚本来在我的网站上接收邮件和联系表单,它工作正常,但是在将消息提交到带有空白消息(例如“谢谢”)的空白页后,将其重定向。

为了防止我使用main.js有下一行:

var form = $('#main-contact-form');
    form.submit(function(event){
        event.preventDefault();
        var form_status = $('<div class="form_status"></div>');
        $.ajax({
            url: $(this).attr('action'),
            beforeSend: function(){
                form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
            }
        }).done(function(data){
            form_status.html('<p class="text-success">Thank you for contact us. As early as possible  we will contact you</p>').delay(3000).fadeOut();
        });
    });

实现它后,重定向问题消失了,但我停止通过此表单接收消息。 为了清楚起见,这是我一直用于发送消息的PHP脚本:

<?php
if(isset($_POST['email'])) {
$email_to = "arman932@gmail.com";
$email_subject = "Summarized propose of the email";
//Errors to show if there is a problem in form fields.
function died($error) {
    echo "We are sorry that we can procceed your request due to error(s)";
    echo "Below is the error(s) list <br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
       !isset($_POST['last_name']) ||
       !isset($_POST['email']) ||
       !isset($_POST['telephone']) ||
       !isset($_POST['comments'])) {
    died('We are sorry to proceed your request due to error within form entries');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
   $telephone = $_POST['telephone']; // 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 .= 'You entered an invalid email<br />';
 }
$string_exp = "/^[A-Za-z .'-]+$/";
 if(!preg_match($string_exp,$first_name)) {
$error_message .= 'Invalid first name<br />';
 }
 if(!preg_match($string_exp,$last_name)) {
$error_message .= 'Invalid Last name<br />';
 }
 if(strlen($comments) < 2) {
$error_message .= 'Invalid comments<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 .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\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);
?>

当我用上面编写的代码行关闭main.js时,它开始工作,但提交后将我重定向到另一个页面,再加上诸如“电子邮件发送”之类的要素,问题检查也消失了。 因此,我想让我的js代码与PHP一起使用,既可以接收消息,又可以拥有美观的电子邮件表单。

只是为了更多地理解,这是我联系表 与main.js相连的代码:

  <form  id= "main-contact-form" name="contactform" method="post" action="send.php">
              <table width="450px">
              <tr>

               <td valign="top">
                <input  type="text" class="form-control" name="first_name" maxlength="50" size="30" placeholder="Name">
               </td>
              </tr>
              <tr>
               <td valign="top">
                <input  type="text"  class="form-control" name="last_name" maxlength="50" size="30" placeholder="Last name">
               </td>
              </tr>
              <tr>
               <td valign="top">
                <input  type="text" class="form-control" name="email" maxlength="80" size="30" placeholder="E-mail">
               </td>
              </tr>
              <tr>

               <td valign="top">
                <input  type="text"  class="form-control" name="telephone" maxlength="30" size="30" placeholder="Phone">
               </td>
              </tr>
              <tr>
               <td valign="top">
                <textarea  name="comments"  class="form-control" maxlength="1000" cols="25" rows="6" placeholder="Put your message here"></textarea>
               </td>
              </tr>
              <tr>
               <td colspan="2" style="text-align:center">
                <input type="submit" class="btn-submit" value="Submit">
               </td>
              </tr>
              </table>
              </form>

0 个答案:

没有答案