2016-08-04 44 views
1

我想在我的網站上提交表單後運行一些php代碼。它重定向到另一個頁面來發送信息,然後使用header()返回到上一頁。我想要發生的是加載一條消息,顯示消息已成功發送,但我不確定如何在從另一個頁面重定向後執行此操作。我希望使用php或jquery來實現這一點,並且我將包含我在下面使用的代碼。我試圖使用session_start函數來執行此操作,但在用戶離開後,回顯的文本仍保留在頁面上。在表單提交併重定向後運行PHP代碼

<form method="post" action="mail.php"> 
<tr> 
<td class="label">Name:</td> 
<td class="input"><input type="text" maxlength="40" name="name" pattern="[a-zA-Z0-9]+" title="Please enter your name." required></td> 
</tr> 
<tr> 
<td class="label">Email:</td> 
<td class="input"><input type="email" maxlength="24" name="email" title="Please enter an email address." required></td> 
</tr> 
<tr> 
<td class="label">Subject:</td> 
<td class="input"><input type="text" maxlength="24" name="subject" title="Please enter a subject." required></td> 
</tr> 
<tr> 
<td class="label">Message:</td> 
<td class="input"><textarea rows="9" maxlength="1000" name="message" title="Please enter a message." required></textarea></td> 
</tr> 
<tr> 
<td></td> 
<td> 
<input type="submit" value="Submit"> 
</td> 
</tr> 
</form> 

<?php 
$to = "[email protected]"; 
$subject = "Message From Your Website: ".trim(htmlspecialchars($_POST["subject"])); 
$message = trim(htmlspecialchars($_POST["message"])); 
$headers = "Reply To: ".trim(htmlspecialchars($_POST["email"]))."" ."\r\n". "From: " .trim(htmlspecialchars($_POST["name"])).""; 
mail($to,$subject,$message,$headers); 
header("Location: index.php#contact"); 
?> 

回答

0

簡單。

header("Location: index.php?thankyou#contact"); 

然後爲isset($_GET['thankyou'])做一個條件並打印出一條消息。

+0

完美,這幫助了很多。 –

+0

@JordanU。非常高興能夠得到幫助:) –

0

而不是使用PHP的,使用jQuery(既然你提到它),並使用它的潛力來發送表單數據,使用AJAX POST請求。通過這種方式,您可以保持同一頁面而無需導航到另一個頁面(因爲您正在返回),並且您可以在發佈請求完成後顯示消息,一旦請求完成,ajax POST方法會返回回調 Read about it here

0

根據你的描述:表單頁面 - PHP電子郵件頁面 - 回表單頁面,則只需添加php的電子郵件部分插入表單頁面:

的index.php:

<?php 
$data = init_data(); 
if ($data['cmd']) { 
    $err = main_send($data); 
} 
if (!$data['cmd'] || $err) { 
    main_form($data, $err); 
} 

function init_data() { 
    $arr = ['cmd', 'name', 'email', 'subject', 'message']; 
    foreach ($arr as $k) { 
     $data[$k] = isset($_POST[$k]) ? trim(htmlspecialchars($_POST[$k])) : ''; 
    } 
    return $data; 
} 

function main_send($data) { 
    if (!$data['name']) { 
     return 'What is your name?'; 
    } elseif (!$data['email']) { 
     return 'What is your email?'; 
    } elseif (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) { 
     return 'Your email is not valid'; 
    } elseif (!$data['message']) { 
     return 'Please say something'; 
    } 

    $to = '[email protected]'; 
    $subject = 'Message From Your Website: ' . $data['subject']; 
    $message = trim(htmlspecialchars($_POST["message"])); 
    $headers = 'Reply To: ' . $data['email'])) . "\r\nFrom: " . $data['name'])); 
    mail($to, $subject, $message, $headers); 
    echo '<p>Thank you very much for the feedback</p>'; 
} 

function main_form($data, $err = '') { 
    if ($err) { 
     echo '<p class="err">' , $err , '</p>'; 
    } 
    echo 'your original form html here ...'; 
} 
0

試試這個代碼:

表單代碼

<div id="message"></div> 
<form method="post" id="email-form" action=""> 
<tr> 
<td class="label">Name:</td> 
<td class="input"><input type="text" maxlength="40" id="name" name="name" pattern="[a-zA-Z0-9]+" title="Please enter your name." required></td> 
</tr> 
<tr> 
<td class="label">Email:</td> 
<td class="input"><input type="email" maxlength="24" id="email" name="email" title="Please enter an email address." required></td> 
</tr> 
<tr> 
<td class="label">Subject:</td> 
<td class="input"><input type="text" maxlength="24" id="subject" name="subject" title="Please enter a subject." required></td> 
</tr> 
<tr> 
<td class="label">Message:</td> 
<td class="input"><textarea rows="9" id="message" maxlength="1000" name="message" title="Please enter a message." required></textarea></td> 
</tr> 
<tr> 
<td></td> 
<td> 
<input type="submit" value="Submit"> 
</td> 
</tr> 
</form> 

email.php代碼

<?php 

//email.php 

$to = "[email protected]"; 
$subject = "Message From Your Website: ".trim(htmlspecialchars($_POST["subject"])); 
$message = trim(htmlspecialchars($_POST["message"])); 
$headers = "Reply To: ".trim(htmlspecialchars($_POST["email"]))."" ."\r\n". "From: " .trim(htmlspecialchars($_POST["name"])).""; 
mail($to,$subject,$message,$headers); 
echo 'Mail was successfully send'; 

?> 

jQuery代碼:

<script> 

$(document).ready(function() { 
    $("#email-form").submit(function(event){ 
     event.preventDefault(); 
     var email= $('#email').val(); 
     var name= $('#name').val(); 
     var subject= $('#subject').val(); 
     var messege = $('#messege').val(); 
     $.ajax({ 
      url: "email.php", 
      type:"POST", 
      data: 'email='+email + '&name='+name +'&subject='+subject+'&message='+message, 
      success: function(data){ 
      $('#message').html(data);  
      setTimeout(function(){ //redirect after 5 sec 
      window.history.go(-1); // Simulates a back button click 
      },5000); // time in milli sec 
      } 

      }); 
     }); 
    }); 

</script>