我正在创建提交联系表单。但是,当按下提交按钮时,我希望它返回到我的contact.php页面,并在正文页面的顶部添加一条消息,例如。 “我们已收到您的电子邮件,我们的代理商会尽快与您联系”
我有两个文件,contact.php表单和send_form_email.php用于电子邮件处理
我试过这个
header("location: contact.php");
然而,这只会在没有任何确认的情况下将我发回联系页面
能帮助我吗?
此致 莱克斯
答案 0 :(得分:2)
最简单的方法是查询字符串。
header("location: contact.php?success=1");
或类似的东西。
然后在您的联系页面中,使用$_GET['success']
检查是否已设置(如果已设置)(您可以使用isset()
查看其是否在网址中,或者如果您愿意,请查看实际值做更多)显示你的信息。
对于稍微“复杂”的内容,请参阅:PHP passing messages between pages
答案 1 :(得分:1)
我建议您在表单中使用POST。在这里使用GET是一个坏主意。
http://www.w3schools.com/php/php_forms.asp
然后添加
if($_POST["done"] == 1)
echo "We have received your email, our agent will contact you shortly";
答案 2 :(得分:1)
你有两个选择。
最简单的方法是在同一页面上使用表单处理逻辑,然后将表单的action属性设置为该页面。
你可以做一些事情,比如检查你是否有值发布,告诉你是否需要处理页面。
if (isset($_POST['my_value_from_form'])) {
// process
}
// body of page itself
这可以让你轻松放一个。
另一种方法是通过将GET值附加到URL来添加GET值:
header("location: contact.php?message=1")
并使用$_GET['message']
来确定要显示的内容。但是,?message = 1将在页面的URL中,因此可能不太理想。
另一种方法是在你指导之前设置一个会话值,然后检查那个值是否存在(并在你显示它之后也清除它。
// on send_form_email.php
session_start();
$_SESSION['message'] = 1;
// on contact.php
session_start();
if ($_SESSION['message'] == 1) {
// do something
}
unset($_SESSION['message']); // so it only shows once.
所有方式都有微小的权衡,主要是因为你组织代码的方式。如果我要实现它,我会使用会话方法。
答案 3 :(得分:0)
您需要使用sessions。实施例
session_start(); // start session
$_SESSION['message'] = 'We have received your email, our agent will contact you shortly';
header("location: contact.php");
在contact.php
echo $_SESSION['message'];
unset($_SESSION['message']); // delete message so it doesnt display again
答案 4 :(得分:0)
将标题更改为
header("location: contact.php?success");
然后在contact.php
上添加此页面
if(isset($_GET['success'])){
echo "We have received your email, our agent will contact you shortly";
}
答案 5 :(得分:0)
你可以像这样做一个消息会话
$_SESSION['message'] = "your message";
之后你会重定向将此会话插入到消息div中,然后取消设置
<?php
session_start();
$_SESSION['message'] = "your message";
header("location: contact.php");
不要忘记在您的文件contact.php
之上使用session_start();
开始会话
答案 6 :(得分:0)
在档案Contact.php
添加
$done = $_GET['done'];
if ($done == 1){
echo "<div>We have received your email, our agent will contact you shortly</div>";
}
在档案send_form_email.php
添加
header("Location: contact.php?done=1");