繁体   English   中英

在php表单上创建成功消息

[英]Create success message on php form submit

我正在尝试使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");
    }
  }
?>

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

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";
     }
 }
$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页面中,您必须定义<?php $message ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM