我如何获得我的PHP表单以显示成功通知?

问题描述 投票:1回答:4

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

我刚刚在我的网站上实施了一个引导联系表。如果邮件发送成功,则将该表单设置为重定向到另一个网站。我真的希望它仅显示一条通知,指出“已发送电子邮件”或类似内容。

我确实意识到

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

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

这是我的代码:

PHP

/* Set e-mail recipient */
$myemail = "[email protected]";

/* 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>

提前感谢!

php html contact-form
4个回答
0
投票

我想你可以使用

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

并将其放在合适的位置

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

我想这有点“ hacky”,但是可以。

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


0
投票

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

所以更改:

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

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

和thank_you.php将是:

<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>

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


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);
© www.soinside.com 2019 - 2024. All rights reserved.