This is ridiculous. My problem is as per the title. Trying to var_dump
or print_r
the $_POST superglobal yields an empty array.
Take a look at my form:
<form method="post" action="" class="login">
<fieldset>
<dl>
<dt><label for="loginEmail">Email</label></dt>
<dd><input type="email" id="loginEmail" name="loginEmail" maxLength="32" /></dd>
<dt><label for="loginPassword">Password</label></dt>
<dd><input type="password" id="loginPassword" name="loginPassword" maxlength="64" /></dd>
<dt></dt>
<dd><input type="submit" value="submit" class="button" /></dd>
</dl>
</fieldset>
</form>
I don't see anything wrong with it. The name
attribute is intact, and I'm posting to the same page, yet POST continues to be empty. It's worth noting I have another form on another page with similar syntax which works.
For example:
if (!empty($_POST)) {
echo '42';
}
Does not run.
Any ideas?
Your form is pointing nowhere.. If you expect it to be the same page, then specify,
<form action="{$_SERVER['SCRIPT_NAME']}">
which will render the same page, but send the parameters. if you have a handler script somewhere, then specify the relative url.
Try printing out the $_SERVER
superglobal. Among other things, it contains REQUEST_METHOD
key whose value should help you out...
By the way, an empty action attribute is actually perfectly fine. I've seen it work consistently across even slightly older browsers.
Also, I once wrote a little snippet which is very helpful for debugging...just insert it at the very top of your PHP code:
function on_shutdown(){
echo "<!--
";
echo "
\tHeaders Sent:";
$file = $line = null;
headers_sent($file, $line);
echo "
\t\t$file: $line";
echo "
\tLast Error:";
if(function_exists('error_get_last') && error_get_last())
foreach(error_get_last() as $k=>$v)
echo "
\t\t$k: $v";
elseif($php_errormsg)
echo "
\t\tError: $php_errormsg";
else echo "
\t\tnone";
echo "
\tIncluded Files:";
echo "
\t\t".implode("
\t\t", get_included_files());
echo "
-->";
}
register_shutdown_function('on_shutdown');
while(ob_get_level())ob_end_clean();
ob_implicit_flush(true);
As we know there is a thing called DEFAULT
According to w3c :
action = uri [CT] This attribute specifies a form processing agent. User agent behavior for a value other than an HTTP URI is undefined. source : http://www.w3.org/TR/html401/interact/forms.html
Now back to the point. your code is running great if both of them are on same page:
<form method="post" action="" class="login">
<fieldset>
<dl>
<dt><label for="loginEmail">Email</label></dt>
<dd><input type="email" id="loginEmail" name="loginEmail" maxLength="32" /></dd>
<dt><label for="loginPassword">Password</label></dt>
<dd><input type="password" id="loginPassword" name="loginPassword" maxlength="64" /></dd>
<dt></dt>
<dd><input type="submit" value="submit" class="button" /></dd>
</dl>
</fieldset>
</form>
<?php
if (!empty($_POST)) {
echo '42';
}
?>
and output after filling the values and submitting it i got output as:
42
and when i changed php code to :
<?php
if (!empty($_POST)) {
var_dump($_POST);
}
?>
i got output as:
array (size=2)
'loginEmail' => string 'abc@abc.com' (length=11)
'loginPassword' => string 'qwerty' (length=6)
Clean your cache and restart your web sever. Hope it will work now :)