我已经制作了代码以防止重复发送表单。根据此代码,遵循行动顺序:
1.用户提交邮件表格
2.Page发送表单数据。会话开始。所有表单数据都分配给全局$ _SESSION变量。邮件由函数mail()发送。变量$ _SESSION ['sent'] = true;是为了回应“谢谢你联系我们!”
3.然后“如果”查看$ _SESSION ['sent']并得到它是真的......
然后出现问题!
因为据我所知,下一步行动必须回应“谢谢你联系我们!”。然后unset()必须销毁$ _SESSION ['sent']以防止重复显示“谢谢...”
但它没有发生,整个联系表格显示在屏幕上,如果没有设置$ _SESSION ['sent'],则必须仅显示该情况。
那么,问题是在if(){}中取消设置($ _ SESSION ['sent'])在整个脚本中销毁$ _SESSION ['sent']?
<?php
/* code for mailing and redirecting to the same page */
session_start();
if ( !empty( $_POST['tosendmail'] ) ) {
$_SESSION['admin_email'] = "mymail@mail.com";
$_SESSION['subject'] = $_REQUEST['subject'];
$_SESSION['email'] = $_REQUEST['email'];
$_SESSION['message'] = $_REQUEST['message'];
$send = mail( $_SESSION['admin_email'], $_SESSION['who'], $_SESSION['message'], "From:" . $_SESSION['email'] );
/* create variable $_SESSION['sent'] to use condition if ( isset( $_SESSION['sent'] ) ) below */
if ( $send ) {
$_SESSION['sent'] = true;
}
header( "Location: " . $_SERVER["REQUEST_URI"] );
}
if ( isset( $_SESSION['sent'] ) ) {
?>
<h3 class="page-title">Thank you for contacting us!</h3>
<?php
unset( $_SESSION['sent'] );
} else {
?>
<!-- CONTACT FORM -->
<h3 class="page-title">Contact us:</h3>
<form class="contact-form" method="post">
Your name:<br>
<input type="text" name="subject" value=""><br>
E-mail:<br>
<input type="email" name="email" value=""><br>
Your message:<br>
<textarea type="text" name="message" rows="10" cols="40"></textarea><br><br>
<input type="submit" name="tosendmail" value="Send">
<input type="reset" value="Reset">
</form>
<?php }
?>
答案 0 :(得分:1)
是的,$_SESSION
是一个“超全球”:http://php.net/manual/en/language.variables.superglobals.php
答案 1 :(得分:0)
不确定我会为$ _SESSION打扰那部分。您的服务器知道$ send是否为真。
if (!empty( $_POST['tosendmail'] ) ) {
$sent ='false';
...
...
if ( $send ) {
$sent = 'true'; $display ='Thank you for contacting us!';
} else { $display ='Ooops, something went wrong';}
if (isset($display)){echo $display; }
}