如何在php中提交表单后重定向
How to redirect after submitting form in php
我想在提交带有警告框的表单后重定向相同的表单页面。我该怎么设置请帮忙。。非常感谢
PHP代码在这里
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$contact = $_POST['contact'];
$message = $_POST['message'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'test@website.web.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.'n"."contact number: $contact.'n".
"Here is the message: $message.'n"."Email ID: $visitor_email.'n".
$to = "lead@lead.web.com";//<== update the email address
$headers = "From: $email_from 'r'n";
$headers .= "Reply-To: $visitor_email 'r'n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done.
printf("<script> alert('Thank You!'); </script>");
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('('n+)',
'('r+)',
'('t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
在这段代码中,我只收到了一个警告框,我想在提交表单后重定向同一页面。请在这个上提供帮助
我的HTML是
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>PHP form to email sample form</title>
<!-- define some style elements-->
<style>
label,a
{
font-family : Arial, Helvetica, sans-serif;
font-size : 12px;
}
</style>
<!-- a helper script for vaidating the form-->
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>
</head>
<body>
<!-- Start code for the form-->
<form method="post" name="myemailform" action="form-to-email.php">
<p>
<label for='name'>Enter Name: </label><br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Enter Email Address:</label><br>
<input type="text" name="email">
</p>
<p>
<label for='contact'>Contact Number:</label>
<input type="text" name="contact">
</p>
<p>
<label for='message'>Enter Message:</label> <br>
<textarea name="message"></textarea>
</p>
<input type="submit" name='submit' value="submit">
</form>
<script language="JavaScript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator = new Validator("myemailform");
frmvalidator.addValidation("name","req","Please provide your name");
frmvalidator.addValidation("email","req","Please provide your email");
frmvalidator.addValidation("email","email","Please enter a valid email address");
</script>
</body>
</html>
将其添加到php代码的开头:
header('Location: http://www.example.com/');
相关文章:
- 根据select选项元素将表单重定向到不同的URL
- HTML表单重定向
- 根据选择选项元素将表单重定向到不同的URL,并在电子邮件中获取表单详细信息
- 如何根据选择下拉列表从表单重定向到 URL
- 根据select option元素将表单重定向到不同的URL,但重定向后未选中
- 验证表单+重定向
- 提交的表单重定向到输入URL
- 花式框默认关闭在表单重定向后不起作用
- 如何将选择加入表单重定向到感谢页面
- 基于表单数据提交表单重定向
- 护照 JS - 一个登录表单 - 重定向到不同的 URI,具体取决于用户角色
- 表单重定向成功/失败页面
- 如何将表单数据从eloqua中的表单重定向到登录页
- 有人能总结一下这个cookie/表单重定向的作用吗
- Javascript不会将表单重定向到指定的文件路径(或URL),使用按回车键或单击提交按钮的双重选项
- 表单重定向提交不工作,为什么
- 复选框选择时的表单重定向
- 如何使用javascript从html登录表单重定向到欢迎页面?该网站处于脱机状态,并且位于开发服务器上
- onsubmit:当表单重定向时不提交数据
- 将网站URL表单重定向到特定页面