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