以PHP联系人形式重定向

问题描述 投票:0回答:1

我已经在wordpress上构建了一个PHP联系人表单,该表单根据用户所选择的区域来重定向用户。

我已经使用'www.google.com'作为测试URL。

但是,表单将重定向到我基于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>
php html redirect contact-form
1个回答
0
投票

要在与表单相同的页面上调用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>
© www.soinside.com 2019 - 2024. All rights reserved.