September 27, 2009
Posting A Form to A WordPress Page
I develop quite a few sites in WordPress. Being that many of these sites are not “blogs” and need to behave like a traditional, static website, I’ve come across and worked through several obscure and (understandably) poorly documented issues and quirks in WordPress.
This is one of those stories.
I had a page template with a simple contact form that was posting to # (itself):
<form name="contactform" method="post" action="#"> <input name="name" /> <input name="email" /> <input name="phone" /> <textarea name="comment"></textarea> <input type="submit" name="submit" /> </form>
Looked great, but when I submitted the form, instead of reloading the page with the form posted, WordPress would display the 404 error page. Looking at the address bar, the URL was identical (except with a # at the end), and if I reloaded the page without reposting the form data, the unsubmitted page would load fine. I was both baffled and maddened.
As a last, desperate resort, I decided to change the names of the form fields, after which it worked perfectly. Being that I’d worked on that issue for an hour, it was a frustrating relief.
I didn’t know this at the time, but WordPress uses POST variables (the same kind that you pass when you submit a form) in the secret hocus pocus that goes on in the background as a page loads. And my POST variables were conflicting with them.
So, I just added “form” in front of all the field names, and it was golden:
<form name="contactform" method="post" action="#"> <input name="formname" /> <input name="formemail" /> <input name="formphone" /> <textarea name="formcomment"></textarea> <input type="submit" name="submit" /> </form>
So the moral of the story is this:
If you are posting a custom form anywhere within WordPress, use very unique field names so that they don’t conflict with WordPress’ variables. Otherwise, when you submit the form, it will have errors displaying the page, and instead display the 404 error page.
I think I just saved you an hour.
Great… you saved me an hour.
Thanks..
Gr8 tip! Thanks ;)
I reckon you’ve saved me several hours – many thanks!
Great! Thanks, I was banging my head against my desk trying to get this contact form working!
Thank you! This was starting to drive me nuts, thanks for sharing
Thanks a lot, this saved me a lot of time and frustration.
This worked for me! Thank you, thank you, thank you!
Thanks for posting, solved the problem I was having.
Unfortunately I wasted about an hour trying to figure it out on my own. Fortunately, I think you saved me a few more, plus some hair.
Thanks.
Thanks a lot. Perfect!
Thanks Caleb. Your solution worked a treat!
Thanks for that – I’m sure this will come in handy in the future.
I’m kind of pissed the solution was so easy, but very grateful that the error is fixed. Thank you very much!
Just repeating what everyone else said, but I wasted so much time with this stupid thing. Thank you!!
You are my saviour!!! I feel like an idiot for changing the fields to ‘name’ and ‘email’ instead of ‘your_name’ and ‘your_email’ just because I thought they “looked cleaner”… won’t do that again! Thank you!
Excellent, thanks!
Thank you for starting this excellent thread. I’ve been looking info like this.
My hero! I thank you for that hour you saved! :)
This worked for me also. Thanks.
Thanks Mr. Smart.
I wasted about an hour on this then found your solution, THANKYOU so much!!!
Thank you so much for pointing me to this! Now (after trying different things for an hour) my form works like a charm.
Wow,
You have just saved me more than just an hour. I would have never thought of this.
Worked a charm.