Solution 1 :
To call your checkRegion
function on the same page as the form then unless this page is customepage1.php
you need to remove the action from the form and pass the POST variable Region
as the input parameter to your function. As you are calling header
function you should invoke this function before any HTML content has been generated ( unless you have output buffering
enabled )
<?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>
Solution 2 :
<?php
if(isset($_POST['submit'])){
$region = $_POST['Region'];
switch ($region ) {
case 'North': $url = 'https://www.google.com/'; break;
case 'South': $url = 'https://www.google.com/'; break;
case 'East': $url = 'https://www.google.com/'; break;
case 'West': $url = 'https://www.google.com/'; break;
}
if(!empty($url)){
header('Location: '+ $url);
}
}
?>
<html>
<head>
<title> Meal Planner </title>
</head>
<body>
<form action="<?php the_permalink(); ?>" 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' name="submit" value='SUBMIT'><input type='reset' value='CLEAR'>
</form>
</body>
</html>
Problem :
I have build a PHP contact form on wordpress that redirects user based upon his/her selected region.
I have used ‘www.google.com’ as a test URL.
However, the form is getting redirected to the custom theme page that I built on WP instead.
Where am I going wrong?
Please find the code below:
<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>
Comments
Comment posted by brombeer
$Region
Comment posted by tipsandtricks-hq.com/introduction-to-wordpress-nonces-5357
Used WordPress nonces to retrieve your form values (
Comment posted by Bot123
Hi, thanks for your response. However, when I am applying this code, I am getting 404 error.
Comment posted by Professor Abronsius
did it help or still having issues?
Comment posted by Bot123
still having issues..getting page not found error…is it because the way the header function is called…do i need to rework on the synatx?
Comment posted by Professor Abronsius
are you saying it can’t find google or another url, as yet, undisclosed to us?
Comment posted by Professor Abronsius
I have just tested exactly what I posted above and it works fine. Note that I did make a change above as I had forgotten to use
Comment posted by mydailysugar.info/meal-tester-page
Hi…however i am getting a 404 error with the same code….is it because I am using it in wordpress? you can check it here:
Comment posted by wordpress.stackexchange.com/questions/11749/…
try rename name fields to
Comment posted by mydailysugar.info/meal-tester-page
Hi, i did…now instead of giving 404 error, it is staying on the same page. check:
Post navigation