我有一个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>