0

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?

marked-down
  • 9,958
  • 22
  • 87
  • 150
  • I assume that `action=""` is not how it actually appears in your code, right? – Dave Aug 25 '13 at 01:17
  • 1
    What you posted is just a form. Where is the handler for it? Plus, if you're using an `if(isset... submit`, then you need to name your submit button as well, if that's what the problem is. – Funk Forty Niner Aug 25 '13 at 01:17
  • Your handler can look something to the affect of `` etc. or to make things simpler, `foreach ($_POST as $variable => $value) {` will output all values taken from a POST variable. – Funk Forty Niner Aug 25 '13 at 01:20
  • @Fred-ii-, yes, I have a complicated handler that I've excluded because it's not relevant. `print_r($_POST)` reveals nothing, and an if statement with code inside dependent on whether $_POST is !empty does not run. – marked-down Aug 25 '13 at 01:22
  • @Antilogical Your form alone (for me) is not enough. Maybe someone else can help you with what you posted, but not me. – Funk Forty Niner Aug 25 '13 at 01:23
  • @Dave, yes it is how it appears in my code. The alternative is to exclude the action attribute all together because it submits to the same page. – marked-down Aug 25 '13 at 01:24
  • Then you should remove it instead of explicitly setting it. Browsers are known to do goofy stuff with blank actions. – Dave Aug 25 '13 at 01:24
  • @Dave. Removed, no difference. $_POST is still empty. It's worth noting I have a syntactically similar form on another page which POST's perfectly which does use `action=""` – marked-down Aug 25 '13 at 01:26
  • @Antilogical For one thing, your `if (!empty($_POST) {` is missing a `)` as is `if (!empty($_POST)) {` – Funk Forty Niner Aug 25 '13 at 01:26
  • @Antilogical then you should try to identify what is different between the pages. If it works on one but not the other, you can't have posted enough information here for us to debug it. Possibly it's to do with the DOCTYPE or encoding, or your script has a syntax error or some other bug, or your HTML is malformed. The list of possibilities goes on. – Dave Aug 25 '13 at 01:30
  • @Antilogical You need to post your entire code and not just pseudo code. The problem may lie elsewhere. I deleted my answer btw. – Funk Forty Niner Aug 25 '13 at 01:33
  • My current odds-on-favourite: Maybe your script has a syntax error later and is therefore not running at all? Try making it output something even before checking the POST. – Dave Aug 25 '13 at 01:34
  • @Dave, I've commented out everything irrelevant. The only things which are on my page now are my form and a simple if conditional which checks if $_POST is empty. – marked-down Aug 25 '13 at 01:35
  • 1
    @Antilogical Your posted code works, for me anyway, using both `if(!empty...` and `if(empty...`. So, in not posting your entire code, I for one cannot help you. Good luck, and I wish you well. – Funk Forty Niner Aug 25 '13 at 01:36
  • @Antilogical You can try this `var_dump($_POST);` best I can suggest. You can't say I haven't tried to help. Now am out of options without seeing full/actual code. – Funk Forty Niner Aug 25 '13 at 01:40

3 Answers3

1

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 "<!--\n";
    echo "\n\tHeaders Sent:";
        $file = $line = null;
        headers_sent($file, $line);
        echo "\n\t\t$file: $line";
    echo "\n\tLast Error:";
        if(function_exists('error_get_last') && error_get_last())
            foreach(error_get_last() as $k=>$v)
                echo "\n\t\t$k: $v";
        elseif($php_errormsg)
            echo "\n\t\tError: $php_errormsg";
        else echo "\n\t\tnone";
    echo "\n\tIncluded Files:";
        echo "\n\t\t".implode("\n\t\t", get_included_files());
    echo "\n-->";
}
register_shutdown_function('on_shutdown');
while(ob_get_level())ob_end_clean();
ob_implicit_flush(true);
Christian
  • 27,509
  • 17
  • 111
  • 155
0

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 :)

Rahul
  • 1,181
  • 1
  • 11
  • 20
-1

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.

ddavison
  • 28,221
  • 15
  • 85
  • 110
  • http://stackoverflow.com/a/280076/1700963 I'd recommend using `SCRIPT_NAME` instead of `PHP_SELF` – Class Aug 25 '13 at 01:33