[英]how to redirect html page form php page?
此代碼中的兩個文件index.html
和mail_handler.php
可打開mail_handler.php
文件,但我不想打開該文件,直接打開HTML文件index.html
並顯示一條消息。
提交!
看到快速的"email sent
”消息后,我希望將用戶從mailhandler_auto.php
定向回index.html
頁面。
mail_handler.php
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['subject'];
$msg=$_POST['msg'];
$to='xyz@yahoo.com'; // Receiver Email ID, Replace with your email ID
$subject='Form Submission';
$message="Name :".$name."\n"."Phone :".$phone."\n"."Subject :".$subject."\n"."Wrote the following :"."\n\n".$msg;
$headers="From: ".$email;
enter code here
if(mail($to, $subject, $message, $headers)){
echo '<script>alert("insert successfull");</script>';
}
else{
echo "Something went wrong!";
}
}
?>
這是我使用過的操作方法的index.html
文件,但是在這段代碼中,該代碼在mail_handler.php
頁面上移動,但是我想轉到index.html
頁面index.html
<form action="mail_handler.php" method="post" name="form" class="">
<div class="form-group ">
<div class="">
<input class="form-control input-lg" type="text" name="name" placeholder="Name*" required >
</div><br>
<div class="">
<input class="form-control input-lg" type="email" name="email" placeholder="Email Address*" required >
</div><br>
<div class="">
<input class="form-control input-lg" type="subject" name="subject" placeholder="Subject*" required >
</div><br>
<div class="">
<textarea class="form-control" name="msg" placeholder="Message*" required rows="8" cols="80"></textarea>
</div><br>
<div class="">
<button class="btn btn-danger btn-lg" type="submit" value="send" name="submit">SUBMIT</button>
</div>
</div>
</form>
發送郵件后,您可以通過兩種方式重定向頁面
1)像使用JavaScript一樣在此處使用以顯示警報。 您可以在這里簡單地添加下面給出的腳本。 它將重定向
window.open("index.html","_parent");
2)通過使用PHP,您也可以執行相同操作。 下面是示例,請將腳本添加到if循環中即可
header('Location: index.html');
要么
header('Location: ./');
請嘗試這個。
在html的標題標簽中嘗試一下:
`<meta http-equiv="refresh" content="5;url=http://www.example.com/page2.php">`
這將在5秒后將其重定向。 有關更多信息,請參見此處 。
正如其他答案中已經建議的那樣,您可以使用window.open通過javascript進行重定向:
window.open("index.html","_self");
或通過帶有header()的 php
header('Location: index.html');
您也可以選擇在同一文件中執行所有操作:
$message=NULL;
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['subject'];
$msg=$_POST['msg'];
$to='xyz@yahoo.com'; // Receiver Email ID, Replace with your email ID
$subject='Form Submission';
$message="Name :".$name."\n"."Phone :".$phone."\n"."Subject :".$subject."\n"."Wrote the following :"."\n\n".$msg;
$headers="From: ".$email;
if(mail($to, $subject, $message, $headers)){
$message= '<script>alert("insert successfull");</script>';
}
else{
$message "Something went wrong!";
}
}
<form action="index.php" method="post" name="form" class="">
<div class="form-group ">
<div class="">
<input class="form-control input-lg" type="text" name="name" placeholder="Name*" required >
</div><br>
<div class="">
<input class="form-control input-lg" type="email" name="email" placeholder="Email Address*" required >
</div><br>
<div class="">
<input class="form-control input-lg" type="subject" name="subject" placeholder="Subject*" required >
</div><br>
<div class="">
<textarea class="form-control" name="msg" placeholder="Message*" required rows="8" cols="80"></textarea>
</div><br>
<div class="">
<button class="btn btn-danger btn-lg" type="submit" value="send" name="submit">SUBMIT</button>
</div>
</div>
然后您可以創建一個條件
if ($message) {
echo $message;
} else {
//show the form
}
顯示表格或消息
請注意,在一個文件中使用所有邏輯可能更容易,更快捷,但在刷新頁面時易於重復發送。
查看Post / Redirect / Get模式以了解更多信息
嘗試
echo '<script>location.replace("index.html");</script>';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.