0

I have created a page template in WordPress to display a form with action="". Very simply, it will not post any of the form data upon submission. I have tried var_dump($_POST) but it just gives me array(0) {} every single time. I've tried examples provided by some websites for creating a form, but I can't reproduce the same results.

I have thoroughly search for answers to this problem but nothing seems to work. It must be something internal or very advanced. I just want to know why my form data won't post and where it might be going.

Sean
  • 55
  • 1
  • 2
  • 5
  • just tried it. still gave me the empty array – Sean Sep 05 '12 at 04:04
  • Is that submitting to the right script ? – The Alpha Sep 05 '12 at 04:09
  • Check that there isn't a post-redirect-get in place. The post data may be there, but it'll be gone after the redirect. – Marc B Sep 05 '12 at 04:10
  • @MarcB how can I do that? I have a page template (ex. temp.php) which I am using for my page. The form and the PHP script are in that template file. The form action is to the same page. Somewhere in the submission the data disappears or gets cleared. – Sean Sep 05 '12 at 04:23
  • @Sean: you can use the developer tools of your browser to detect a redirect. If you're using Firefox you can install Firebug. IE and Chrome come with developer tools by default (I'm not sure with IE, maybe you have to install the "Internet Explorer Developer Toolbar"). – vstm Sep 05 '12 at 04:51
  • The first question I would like to know is why are using an empty action attribute in your form when its against HTML5 specifications (http://stackoverflow.com/questions/1131781/is-it-a-good-practice-to-use-an-empty-url-for-a-html-forms-action-attribute-a). Have you tried submitting the form in other browsers (ie: Chrome, IE)? – SameOldNick Sep 05 '12 at 06:15
  • Whilst I'm not convinced it's the the source of your problem, you shouldn't use an empty action attribute - it can cause unpredictable behaviour and is against the specs. Either don't use it at all, or give it a URL (in PHP, you could use $_SERVER['REQUEST_URI']). – Mark Sep 05 '12 at 09:44
  • Thanks everyone for your help. I tried a very simple form on a page on my friend's WordPress site and it worked fine when I used `var_dump`. Tried the same thing on mine: no go. @Mark I tried your suggestion: `$_SERVER['REQUEST_URI']` and it worked. Although I don't fully understand the cause of the problem, at least now it's solved. I'll be sure to use that statement in the action attribute from now on. – Sean Sep 06 '12 at 08:03

1 Answers1

1

Check your data using a non-WP page, meaning put a php file in web-root, and have the form action point to it, such as ...action="/formtest.php", and on that page:

<?php 
 var_dump($_POST);
?>

This will test if you really aren't getting the POST vars. Then, since you'll 99% likely find they ARE being posted, the likely trouble is that your page IS being redirected somewhere by the theme as mentioned elsewhere (e.g. my theme includes a hook on 'template_redirect'...).

If you are theme savvy and just looking for a quick fix- at the expense of losing it on theme upgrade- you can create an exception just for your temp.php page, but the way that many developers handle it is via Plugins, or handle your form via Ajax. Many others have encountered this same issue, and so, either tacking on code to a Plugin, or performing in-page Ajax (since you are submitting to SELF anyway...).

This page will give you a leg-up on WP Ajax, it's a reputable and well written doc.

bellasys
  • 255
  • 1
  • 8