【问题标题】:Create success message on php form submit在 php 表单提交上创建成功消息
【发布时间】:2013-09-23 19:12:42
【问题描述】:

我正在尝试使 php 邮件表单在成功提交后显示成功消息。 我是 php 新手,我不知道如何使这项工作。 就像它现在一样,提交后,网站只是在页面顶部重新加载。我希望它在联系人 div 处刷新并显示成功消息。

联系div html:

<div class="contact-wrapper" >
  <div class="contact">
    <div class="contact-left" id="contact">
        <?php
        if (isset($_REQUEST['email'])) {
            echo "Thank you for your message.";
        }
        $mail_form = include('php/mail_form.php'); ?>
    </div> <!-- end div contact -->

PHP 联系方式:

<?php
function spamcheck($field)
  {
  //filter_var() sanitizes the e-mail
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);

  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }

if (isset($_REQUEST['email']))
  {//if "email" is filled out, proceed

  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST['email']);
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
  else
    {//send email
    $email = $_REQUEST['email'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    mail("idc615@gmail.com", "Subject: $subject",
    $message, "From: $email" );
    header("location: index.php");
    }
  }
?>

【问题讨论】:

  • 如果你想刷新而不重新加载整个页面,你应该使用 AJAX
  • 如果 div 位于 html(静态)文件中,则不能这样做(没有 AJAX)。如果它是一个 php 文件 - 您可以将参数传递给将用于显示消息的页面。
  • 请也包含您的 HTML 标记。
  • I'd like it to refresh at the contact div -- 我不明白为什么这里的每个人都建议使用 AJAX。
  • header(location) 之后,您不再拥有Request[email]。尝试header("location: index.php?success=1"); 并使用if (isset($_GET['success'])) { echo "Thank you for your message."; }

标签: php html forms email


【解决方案1】:

试试这个代码...您需要暂停脚本一段时间,以便用户可以看到成功或失败消息。没有睡眠用户将无法看到消息,因为标头将立即执行。

if(mail("idc615@gmail.com", "Subject: $subject",$message, "From: $email" )){
   echo "Thank you for your message";
   sleep(2); // sleep for 2 seconds. Enough to display success message
   //header will execute after 2000ms(2s)
   header("location: index.php");
}else{
   echo "Unable to send message";
   sleep(2); // sleep for 2 seconds 
   header("location: index.php");
}

或尝试下面给出的代码:

在 PHP 中联系

if(mail("idc615@gmail.com", "Subject: $subject",$message, "From: $email" )){
   header("location: index.php?status=1");
}else{ 
   header("location: index.php?status=0");
}

在 index.php 中

 if(isset($_GET['status'])){
     $status = $_GET['status'];
     if($status == 1){
        echo "Thank you for your message";
     }else if($status == 0){
        echo "Unable to send message";
     }
 }

【讨论】:

  • 真的是第一次听到这个睡觉,你介意解释一下吗?
  • sleep 暂停执行 php 脚本一段时间(时间作为参数传递)...php.net/manual/en/function.sleep.php。上面的脚本将暂停 2 秒,然后从 header() 开始执行..
  • 好的,但是。我认为发送输出时我们无法更改标题。
  • 顺便说一句,如果你查看手册,你会得到输入是秒的,所以我认为你的意思是 2 而不是 2000
  • 页面永远不会加载此代码。我什至将 2000 更改为 2 仍然无法加载。
【解决方案2】:
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$message = mysql_real_escape_string($_POST['message']);

if(isset($_POST['contactrgtr']))
{
$query = "insert into contact_us(name,email,message)values('$name','$email','$message')";
$res = mysql_query($query);
if($res){
     $message = 'Your Message Successfully Sent';
     include_once('contact.php'); } ` 

然后在contact.php页面中你必须定义&lt;?php $message ?&gt;

【讨论】:

    猜你喜欢
    • 2012-05-10
    • 2021-10-26
    • 1970-01-01
    • 2017-11-17
    • 2015-10-16
    • 2016-10-11
    • 2015-07-13
    • 2013-05-03
    • 2016-03-19
    相关资源
    最近更新 更多