简体   繁体   English

在php表单上创建成功消息

[英]Create success message on php form submit

I'm trying to make a php mail form display a success message upon a successful submit. 我正在尝试使php邮件表单在成功提交后显示成功消息。 I'm new to php and I'm not sure how to make this work. 我是php的新手,我不确定如何使它工作。 As it sits now, after submit, the site just reloads at the top of the page. 现在,提交后,该站点仅在页面顶部重新加载。 I'd like it to refresh at the contact div and display a success message. 我希望它在联系人div刷新并显示成功消息。

Contact div html: 联系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 -->

Contact from PHP: 来自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");
    }
  }
?>

Try this code... You need to halt your script for some time so that user can see success or failure message. 尝试此代码...您需要暂停脚本一段时间,以便用户可以看到成功或失败消息。 Without sleep user will not be able to see message as header will execute immediately. 没有睡眠,用户将无法看到消息,因为标头将立即执行。

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");
}

or try code given below: 或尝试下面给出的代码:

in Contact from 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");
}

in index.php 在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'); } ` 

Then in contact.php page you have to define <?php $message ?> . 然后在contact.php页面中,您必须定义<?php $message ?>

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

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