I have the following form on a wordpress template page. I'm getting a 404 error each time i submit the form but I'm not using any of the reserved workpress parameter names in the form.
<?php
/**
* Template Name: Registration Template
*/
if(isset($_POST['form-submitted']))
{
if(trim($_POST['runner']) === '') {
$runnerError = 'Please enter runner runner.';
$hasError = true;
} else {
$runner = trim($_POST['runner']);
}
if(trim($_POST['racenumber']) === '') {
$numberError = 'Please enter a race number.';
$hasError = true;
} else {
$racenumber = trim($_POST['racenumber']);
}
$race = trim($_POST['race']);
error_log($race.' '.$runner.' '.$racenumber);
$registrationSubmitted = true;
}
get_header();
echo "<pre>GET "; print_r($_GET); echo "</pre>";
echo "<pre>POST "; print_r($_POST); echo "</pre>";
?>
<div id="container">
<?php
if(isset($registrationSubmitted) && $registrationSubmitted == true)
{
echo '<div class="thanks"><p>The runner has been registered.</p></div>';
}
else
{
$races = // magic array
$selectRaces = '<select name="race" id="race">';
foreach($races as $racez)
{
$selectRaces .= sprintf('<option value=%d>%s</option>',$race->id,$race->name);
}
$selectRaces .= '</select>';
echo apply_filters('the_content','
<form action="'.get_permalink().'" id="form" method="POST">
[one_half last="no"]
<b>Race Details</b><br/>
RaceNumber<input type="text" name="racenumber" id="racenumber"/><br/>
Race'.$selectRaces.'<br/>
[/one_half]
[one_half last="yes"]
<b>Runner Details</b><br/>
ID<input type="text" name="runner" id="runner"/><br/>
Firstname<input type="text" name="first" id="first"/><br/>
Surname<input type="text" name="last" id="last"/><br/>
Gender<input type="text" name="gender" id="gender"/><br/>
DOB<input type="text" name="dob" id="dob"/><br/>
Standard<input type="text" name="standard" id="standard"/><br/>
Company<input type="text" name="company" id="company"/><br/>
[/one_half]
<input type="submit" value="Register Runner"/>
<input type="hidden" name="form-submitted" id="bhaa-submitted" value="true" />
</form>');
}
echo '</div>';
?>
<?php get_footer(); ?>
I've customised my 404 page to dump the $_POST values so i'm sure the parameter values are being submitted.
[racenumber] => 5
[race] => 2596
[runner] => 5
[first] =>
[last] =>
[gender] =>
[dob] =>
[standard] =>
[company] =>
[form-submitted] => true
Can anyone explain my the logic in my 'isset($_POST['form-submitted'])' is not being exercised?
The generated html
<form action="http://localhost/registration/" id="form" method="POST">
<div class="one_half">
<b>Race Details</b><br><br>
RaceNumber<input name="number" id="number" type="text"><br><br>
Race<br>
<select name="race" id="race">
<option value="2596" id="2596">4-Mile-M</option>
<option value="2595" id="2595">2-Mile-W</option>
</select>
EDIT
I've changed the code where is set the values of the select dropdown to use an incrementing int value rather than using sprintf to map a string value to an int value. When the first element is selected the form submits, if the second option is picked i get a 404!
$races = // magic array
$selectRaces = '<select name="race" id="race">';
$i=0;
foreach($races as $racez)
{
$selectRaces .= sprintf('<option value=%d>%s</option>',$i++,$race->name);
}
To do this, go to Settings » Confirmation like before. This time, select Go to URL (Redirect) from the Confirmation Type dropdown menu. Then, paste the complete URL of the external page you'd like to send the user to in the field labeled Confirmation Redirect URL.
Conclusion. 404 not found is a HTTP status code error that indicates the specific page you are trying to visit is non-existent. Usually, it occurs when the domain points to an incorrect DNS address, files or folders have misconfigured permissions, or the system runs a corrupt . htaccess file.
The error can be resolved with the following steps: Navigate to Pages> All pages> select the page with the form (usually that is 'Contacts'). Locate the shortcode used for the contact form in there. It should be replaced with correct shortcode.
Problem is that WordPress have some words reserved and it will throw that error when submitting forms:
Some of the words that I found myself and surfing the web are:
So be careful when creating a a custom form and try to name your inputs with some prefix. In my case, I had a custom post type called "history" and I was naming the input the same way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With