如何返回到上一页后提交表单与php
How to return to the previous page after a submit form with php?
我有一个html表单,其"action"属性调用contact.php文件。问题是,在我提交表单后,可见的文件是一个地址为contact.php的空白页面,我想再次看到主页面的表单。
HTML:
<form id="myForm" action="php-contact/contact.php"
method="post" class="contact_form" autocomplete="off"
role="form">
<div class="form-group">
<label for="input-subject">subject</label>
<input name="subject" type="text" class="form-control" id="subject" placeholder="your subject" maxlength="20"/>
</div>
<div class="form-group">
<label for="input-name">name</label>
<input name="name" type="text" class="form-control" id="name" placeholder="your name" maxlength="20"/>
</div>
<div class="form-group">
<label for="input-email">email address</label>
<input name="email" type="text" class="form-control" id="email" placeholder="your email" maxlength="40"/>
</div>
<div class="form-group" >
<label for="input-message">message</label>
<textarea name="message" cols="10" rows="10" class="form-control" id="comment" ></textarea>
</div>
<button name="myFormSubmitted" type="submit" class="btn btn-primary btn-lg btn-block">send</button>
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "pep.g@gmail.com";
$message = '
name: '.$name.'
email: '.$email.'
message: '.$message.'
';
$headers = 'From: pep.website@website.com';
if (
mail($to, $subject, $message, $headers)
)
echo"<script>alert('message send succesfully')</script>";
else
echo"<script>alert('message not send')</script>";
?>
使用$_SERVER["HTTP_REFERER"]
或在表单上使用包含当前页面url的隐藏字段:
<form action="myAction.php">
<input type="hidden" name="destination" value="<?php echo $_SERVER["REQUEST_URI"]; ?>"/>
<!-- other form inputs -->
<input type="submit"/>
</form>
myAction.php
<?php
/* Do work */
if(isset($_REQUEST["destination"])){
header("Location: {$_REQUEST["destination"]}");
}else if(isset($_SERVER["HTTP_REFERER"])){
header("Location: {$_SERVER["HTTP_REFERER"]}");
}else{
/* some fallback, maybe redirect to index.php */
}
即使这样,你也应该有回退以防客户端不尊重HTTP重定向,如一些重定向javascript,一个元重定向和一个链接到目的地。
在警报后添加JS重定向,然后
echo "<script>
alert('message sent succesfully');
window.history.go(-1);
</script>";
在contact.php文件中,只需在PHP代码末尾使用如下内容:
header("location:yourfilenamehere.php");
这将重定向到您指定的任何页面。
你可以做两件事…
1)在表单文件中包含php逻辑并提交到该文件。在文件的顶部,放入如下内容:
if(isset($_POST['name'])) {
// your sending logic
}
后面跟着你的表单
2)使用header()函数重定向到您的表单。
header("Location: form.html");
我觉得张贴这个答案很糟糕,因为它只是连接另外两个SO答案。
第一部分Charlie74在本页的回答
// add the message you want to show to the user
header("Location: yourfilenamehere.php?alertmsg=message+send+succesfully");
exit();
第二部分检查查询字符串
中的alertmsg名称参见SO post的getParameterByName:
function getParameterByName(name) {
name = name.replace(/['[]/, "''[").replace(/[']]/, "'']");
var regex = new RegExp("[''?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/'+/g, " "));
}
在您要重定向的页面中调用getParameterByName
函数:
<script>
var msg = getParameterByName('alertmsg');
if (alertmsg.length > 0) {
alert(msg);
}
</script>
相关文章:
- 如何获取不属于我项目的上一页的URL
- 如何在Facebook上的iframe应用程序中使后退按钮返回到上一页
- 使用Booklet Jquery插件,我如何验证最后一页是否显示
- 在表单提交后显示模态,直到加载下一页为止..不适用于狩猎
- 如何在提交时防止粘性复选框重置?(一页上有多个表格)
- jQuery - $.ajax();转到提交时的下一页
- 如果答案正确,如何将提交按钮重定向到下一页
- 如何在多页面中制作一个 JavaScript 表单向导,其中包含下一页,并取消并返回并提交最后一页
- 当点击rails中的提交按钮时,如何在重定向到下一页之前显示模态
- Phantom.js填充字段,点击提交,现在只能访问下一页
- 如何使单选字段在提交时确定下一页
- 如何在使用Ghost.py提交表单后获得下一页
- 提交不应转移到下一页
- 在当前页面,.net上提交web表单后,发送用户回到上一页
- 如何在HTML表单提交后回到上一页
- 在转到下一页之前自动提交表单
- 不要"离开这一页"在提交联系表格时
- 如何返回到上一页后提交表单与php
- HTML / js.需要在一页上分别提交两种不同的表格
- 我如何提交表单,并在同一时间移动到下一页