ind*_*lue 6 html javascript php forms
我在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>";
?>
Run Code Online (Sandbox Code Playgroud)
Nic*_*ert 11
使用$_SERVER["HTTP_REFERER"]或使用表单上的隐藏字段和当前页面的网址:
<form action="myAction.php">
<input type="hidden" name="destination" value="<?php echo $_SERVER["REQUEST_URI"]; ?>"/>
<!-- other form inputs -->
<input type="submit"/>
</form>
Run Code Online (Sandbox Code Playgroud)
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 */
}
Run Code Online (Sandbox Code Playgroud)
然后,即便如此,如果由于某种原因客户端不尊重HTTP重定向,例如一些重定向javascript,元重定向和到目标的链接,你应该有回退.
然后在警报之后添加JS重定向
echo "<script>
alert('message sent succesfully');
window.history.go(-1);
</script>";
Run Code Online (Sandbox Code Playgroud)