如何让我的php表单显示成功通知?

时间:2015-11-26 21:20:11

标签: php html contact-form

这是我在这里的第一篇文章。我真的希望你能帮助我!

我刚刚在我的网站上实现了一个bootstrap联系表单。如果邮件成功发送,表单将设置为重定向到另一个网站。我真的希望它只是显示一个通知,说“发送电子邮件”或类似的东西。

我确实意识到了

header('Location: http://address-of-confirmation-page.html'); exit();

是需要改变的,我只是不知道如何将其改为成功消息

这是我的代码:

PHP

/* Set e-mail recipient */
$myemail = "your-email@gmail.com";

/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "Your Name");
$email = check_input($_POST['inputEmail'], "Your E-mail Address");
$subject = check_input($_POST['inputSubject'], "Message Subject");
$message = check_input($_POST['inputMessage'], "Your Message");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */

$subject = "Someone has sent you a message";

$message = "

Someone has sent you a message using your contac form:

Name: $name
Email: $email
Subject: $subject

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: http://address-of-confirmation-page.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>

HTML

<div class="container">
        <div class="panel panel-default" style="margin:0 auto;width:500px">
          <div class="panel-heading">
            <h2 class="panel-title">Contact Form</h2>
          </div>
          <div class="panel-body">
            <form name="contactform" method="post" action="php/contact.php" class="form-horizontal" role="form">
              <div class="form-group">
                <label for="inputName" class="col-lg-2 control-label">Name</label>
                <div class="col-lg-10">
                  <input type="text" class="form-control" id="inputName" name="inputName" placeholder="Your Name">
                </div>
              </div>
              <div class="form-group">
                <label for="inputEmail1" class="col-lg-2 control-label">Email</label>
                <div class="col-lg-10">
                  <input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Your Email">
                </div>
              </div>
              <div class="form-group">
                <label for="inputSubject" class="col-lg-2 control-label">Subject</label>
                <div class="col-lg-10">
                  <input type="text" class="form-control" id="inputSubject" name="inputSubject" placeholder="Subject Message">
                </div>
              </div>
              <div class="form-group">
                <label for="inputPassword1" class="col-lg-2 control-label">Message</label>
                <div class="col-lg-10">
                  <textarea class="form-control" rows="4" id="inputMessage" name="inputMessage" placeholder="Your message..."></textarea>
                </div>
              </div>
              <div class="form-group">
                <div class="col-lg-offset-2 col-lg-10">
                  <button type="submit" class="btn btn-default">
                    Send Message
                  </button>
                </div>
              </div>
            </form>

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

Bootstrap已经有像这样的成功消息,这将是很好的:

<div class="bs-example">
    <div class="alert alert-success fade in">
        <a href="#" class="close" data-dismiss="alert">&times;</a>
        <strong>Success!</strong> Your message has been sent successfully.
    </div>
</div>

提前致谢!

3 个答案:

答案 0 :(得分:0)

我想你可以使用

header('Location: http://current-page.php?success');

把它放在合适的地方

if(isset($_GET['success'])){
    //echo the success message
}

我想这有点像“hacky”,但它确实有用。

我还建议jQuery将表单提交到外部页面并让它返回成功并让jQuery返回成功标志,然后在页面上动态显示成功消息。您可以对此进行一些研究,而不是使用PHP来完成此任务。

答案 1 :(得分:0)

为什么不将“http://address-of-confirmation-page.html”页面设置为简单的感谢页面?这不会涉及任何聪明的脚本。它基本,但它适合你的脚本!

所以改变:

 group.traverse(function(child) {
    if(child instanceof THREE.Mesh){
          child.material.color.setHex('0xffffff');
    }
 });

    /* Redirect visitor to the thank you page */
header('Location: http://address-of-confirmation-page.html');
exit();

和thank_you.php将是:

    /* Redirect visitor to the thank you page */
header('Location: http://yourwebsite.com/thank_you.php');
exit();

这是回答你的问题,还是解决方案过于简单?

答案 2 :(得分:0)

最好的方法就是使用Ajax,而不是那么难(并且易于维护)。

使用Javascript / jQuery的

首先在表单元素中添加一个id,假设为#contact-form,然后让我们通过ajax发送表单:

$("#contact-form").submit(function(e) {
    $.ajax({
       type: "POST",
       url: php/contact.php,
       data: $("#contact-form").serialize(),
       success: function(data){
          $('#message')
            .addClass('alert-'+ data.status)
            .append(data.message);
       }
    );
    e.preventDefault();
});

HTML

默认隐藏邮件,让我们根据p

显示正确的邮件
    <div id="message" class="alert" style="display: none;">
        <a href="#" class="close" data-dismiss="alert">&times;</a>
    </div>

<!-- Your contact form here -->

PHP

这是你的作业,脚本应该在json中等待响应,例如:

$result = array(
   'status' =>  // success | error ,
   'message' => // 'Success Message' |  'validation message'
);
echo json_encode($result);