2016-08-04 92 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>