2014-09-27 48 views
1

这就是所谓的sendmail.php一个验证并将从接触形式的电子邮件(其中包含如下)PHP文件停止页面重定向到php脚本文件提交表单时

我有麻烦的是,当该警报被点击它重定向到sendemail.php,因为,我假设,形式的行动= sendmail.php

我该如何解决这个问题,因此它留在联系页面上,用户可以拿起他们的地方离开?我曾尝试使用标题为感谢页面,但无济于事!

if (isset($_POST["submit"])) { 
    $validate = validateInput($_POST["name"], $_POST["message"], $_POST["subject"]); 
    if ($validate==FALSE) { 
     $error_msg = "Please fill out all information"; 
     echo '<script type="text/javascript"> 
      alert("'.$error_msg.'"); 
     </script>'; 
    } else { 
     $mailcheck = spamcheck($_POST["email"]); 
     if ($mailcheck==FALSE) { 
      $error_msg = "Invalid email address"; 
      echo '<script type="text/javascript"> 
      alert("'.$error_msg.'"); 
     </script>'; 
     } else { 
     $email = $_REQUEST['email'] ; 
     $message = $_REQUEST['message'] ; 
     $subject = $_REQUEST['subject'] ; 

     mail("[email protected]", $subject, $message, "From: $email"); 
     header("Location: http://www.thankyou.html"); 
     } 
    } 
} 

<form method="post" action="sendmail.php"> 
    <input type="text" name="name" maxlength="50" placeholder="Name" class="contact-standard" /></br></br> 
    <input type="email" name="email" placeholder="Email" class="contact-standard"></input></br></br> 
    <input type="text" name="subject" placeholder="Subject" class="contact-standard"></input></br></br> 
    <textarea name="message" placeholder="Message" class="contact-message"></textarea></br></br> 
    <input type="submit" name="submit" value="Send Message" class="contact-submit"></input></br></br> 
</form> 
+0

只是在这个页面上发生了一切。将代码从'sendmail.php'移到此页面(或在提交时包含)。 – Rasclatt 2014-09-27 04:10:37

+0

成功后在'sendmail.php'中使用头文件 – 2014-09-27 04:12:37

+0

Arif - 我试过这个,但是我只是一直重定向到sendmail.php。标题为成功工作,但由于某种原因没有验证错误 – 2014-09-27 04:14:50

回答

1

试试这个并阅读评论。 评论:每当验证失败时添加此行window.location =“Your-Form-File-Name.php”;

if (isset($_POST["submit"])) { 
    $validate = validateInput($_POST["name"], $_POST["message"], $_POST["subject"]); 
    if ($validate==FALSE) { 
     $error_msg = "Please fill out all information"; 
     echo '<script type="text/javascript"> 
      alert("'.$error_msg.'"); 
       window.location= "Your-Form-File-Name.php"; **//This line added by me** 
**** 
     </script>'; 
    } else { 
     $mailcheck = spamcheck($_POST["email"]); 
     if ($mailcheck==FALSE) { 
      $error_msg = "Invalid email address"; 
      echo '<script type="text/javascript"> 
      alert("'.$error_msg.'"); 
       window.location= "Your-Form-File-Name.php"; ////This line added by me 
**//Added above line** 
     </script>'; 
     } else { 
     $email = $_REQUEST['email'] ; 
     $message = $_REQUEST['message'] ; 
     $subject = $_REQUEST['subject'] ; 

     mail("[email protected]", $subject, $message, "From: $email"); 
     header("Location: http://www.thankyou.html"); 
     } 
    } 
} 
+0

太棒了,工作。谢谢! – 2014-09-27 04:42:58

+0

重定向到谢谢你的工作正常。这就是让我感到困惑的原因,我不知道为什么标题为此而工作,而不是为其他部分。干杯 – 2014-09-27 04:49:29

+0

有没有办法做到这一点,所以它不会从表单中清除数据?我猜AJAX?不知道如何使用它 – 2014-09-27 04:56:54

1

我没有PHP的经验,但我认为你需要returen表单时未经验证的假。

if ($validate==FALSE) { 
    $error_msg = "Please fill out all information"; 
    echo '<script type="text/javascript"> 
     alert("'.$error_msg.'"); 
    </script>'; 
    return false;//Form is not validated stop from being submitted. 
} 
+0

我以为这是早期的情况,但它并没有解决重定向问题 – 2014-09-27 04:22:23

0

这是sendmail.php,对吧?如果你想重定向到contact.php只是header()到那里。您将表单动作设置为sendmail.php,这意味着您也设置为该文件,如果您从表单标记中删除动作支持,它也会执行相同操作。只需将您的标题值更改为contact.php或

print'location.href =''。$ url_to_contact_php。''';

1

使用jQuery验证表单使用BEFORE用户提交表单时,在成功提交时在同一页上发生“谢谢”。这样您就可以使用PHP来处理电子邮件发送。

<?php 
    if(isset($_POST["submit"])) { 
      $email  = $_POST['email'] ; 
      $message = strip_tags($_POST['message']); 
      $subject = strip_tags($_POST['subject']); 

      if(mail("[email protected]", $subject, $message, "From: $email")) { ?> 
      <h3>Thank you, come again!</h3> 
     <?php } 
      else { ?> 
      <h3>An error occurred. My bad!</h3> 
      <?php } 
     } ?> 

<form method="post" action="contact.php" id="emailer"> 
    <input type="text" name="name" maxlength="50" placeholder="Name" class="contact-standard" /></br></br> 
    <input type="email" name="email" placeholder="Email" class="contact-standard"></input></br></br> 
    <input type="text" name="subject" placeholder="Subject" class="contact-standard"></input></br></br> 
    <textarea name="message" placeholder="Message" class="contact-message"></textarea></br></br> 
    <input type="submit" name="submit" value="Send Message" class="contact-submit"></input></br></br> 
</form> 

<style> 
.error { color: red; } 
</style> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script> 

<script> 
$(document).ready(function() { 
    // On submit, validate below fields. 
    $("#emailer").validate({ 
     rules: { 
      name: "required", 
      email: { 
        required: true, 
        email: true 
       }, 
      subject: "required", 
      message: "required" 
     }, 
     messages: { 

      name: "required", 
      email: { 
        required: "Required", 
        email: "must be valid email" 
       }, 
      subject: "required", 
      message: "required" 
     } 
    }); 
}); 
</script> 
+0

我真的很喜欢这样做,它很好地处理验证。我会保留这个参考。谢谢您的帮助! – 2014-09-27 04:45:23

相关问题