php - 在 PHP 联系表单中重定向
问题描述
我在 wordpress 上构建了一个 PHP 联系表单,该表单根据用户选择的区域重定向用户。
我使用“www.google.com”作为测试网址。
但是,表单被重定向到我在 WP 上构建的自定义主题页面。
我哪里错了?
请在下面找到代码:
<html>
<head>
<title> Meal Planner </title>
</head>
<body>
<?php
function checkregion($Region)
{
SWITCH ($Region) {
case "North":
header('location:https://www.google.com/');
break;
case "South":
header('location:https://www.google.com/');
break;
case "East":
header('location:https://www.google.com/');
break;
case "West":
header('location: https://www.google.com/');
break;
}
}
checkregion($Region);
?>
<form action="<?= get_template_directory_uri() ?>/custompage1.php " method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="phone">
<p>Dropdown Box</p>
<select name="Region" size="1">
<option value="North">North</option>
<option value="South">South</option>
<option value="East">East</option>
<option value="West">West</option>
</select>
<br/>
<input type="submit" value="SUBMIT"><input type="reset" value="CLEAR">
</form>
</body>
</html>
解决方案
checkRegion
要在与表单相同的页面上调用您的函数,除非此页面是customepage1.php
您需要从表单中删除操作并将 POST 变量Region
作为输入参数传递给您的函数。当您调用header
函数时,您应该在生成任何 HTML 内容之前调用此函数(除非您已output buffering
启用)
<?php
function checkregion( $Region ) {
switch ($Region) {
case 'North': header('Location: https://www.google.com/'); break;
case 'South': header('Location: https://www.google.com/'); break;
case 'East': header('Location: https://www.google.com/'); break;
case 'West': header('Location: https://www.google.com/'); break;
}
}
if( !empty( $_POST['Region'] ) ) checkregion( $_POST['Region'] );
?>
<html>
<head>
<title> Meal Planner </title>
</head>
<body>
<form method='POST'>
<p>Name</p> <input type='text' name='name'>
<p>Email</p> <input type='text' name='email'>
<p>Phone</p> <input type='text' name='phone'>
<p>Dropdown Box</p>
<select name='Region' size='1'>
<option value='North'>North
<option value='South'>South
<option value='East'>East
<option value='West'>West
</select>
<br />
<input type='submit' value='SUBMIT'><input type='reset' value='CLEAR'>
</form>
</body>
</html>
推荐阅读
- ruby-on-rails - 在路由中插入动态值(类别名称)作为命名空间
- python - 如何在python中提取括号内的第二个十进制数
- mongodb - 如何从聚合中的reduce或map中的文档字段声明变量?
- sql - 在 .value() 调用中引用兄弟的值?
- list - Ocaml - 将函数应用于字符串列表中的每个字符串并返回一个新的字符串列表
- rest - 为什么 REST 端点会被访问两次,并且可以消除第二次访问吗?
- opengl - 遗留 opengl - 渲染立方体地图布局,了解 glTexCoord3f 参数
- reactjs - “错误登录”后如何弹出material-ui小吃栏警报
- jmeter - ForEach 控制器不会运行多个 HTTP 请求
- curl - 如何找到证书 curl 在 Windows 10 中使用的路径?