4

I'm writing a webpage, and am trying to use php to check a value after a form submits. The php is in the same page, as seen below. The $_POST variable is coming in empty after the submit code comes in though. I've looked at the other posts about this question but none of those answers seem to help. Thanks for looking

<?php
session_start();
require_once 'assets/PHP/membership.php';
$membership = new Membership();
var_dump($_SESSION);
var_dump($_POST);

if($_POST && !empty($_POST['username']) && !empty($_POST['pwd']))
{
    $response = $membership->validate_user($_POST['username'], $_POST['pwd']);
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<div id="login">
    <form method="post" action = "">
        <h2>LOG IN:</h2>
            <label>User Name :</label>
            <input type="text" name="username" id = "username"/>
            <label>Password :</label>
            <input type="password" name="pwd" id = "pwd"/>
            <input type="submit" value="Login" id="submit" name="submit"/>
        </form>
</div>
</html>

I'm very new to HTML and PHP so any help I could get would go a long way.

  • 2
    so, what does this function do? `validate_user()`. and how are you echoing `$response`? – Funk Forty Niner Feb 20 '16 at 02:54
  • 2
    Try your `var_dump` after `session_start();` and before the `require_once` ... some scripts sanitize input and then `unset` the variables. –  Feb 20 '16 at 02:57
  • The validate_user() validates that the user is a valid member in the database. But I'm never getting there because $_POST is empty. I tried moving the var_dump after the session start, and it didn't help. Thanks for the suggestion though. – user2615426 Feb 20 '16 at 03:04
  • I tried your code putting the var_dump before session_start(); and it works to me. Have you tried this? – Rodrigo Leite Feb 20 '16 at 03:17
  • I just tried this. I'm not having success. Still empty array. – user2615426 Feb 20 '16 at 03:28
  • 1
    @user2615426 I just tried your code i got this `array(3) { ["username"]=> string(5) "fried" ["pwd"]=> string(7) "chicken" ["submit"]=> string(5) "Login" }` something wrong in your setup not the code – meda Feb 20 '16 at 03:31
  • Do you have a suggestion as to what could be wrong in the setup? I'm really stumped. – user2615426 Feb 20 '16 at 03:35
  • Could be some configuration in your php.ini or even in your webserver, here there are some tips, take a look if it helps you: http://stackoverflow.com/questions/1282909/php-post-array-empty-upon-form-submission – Rodrigo Leite Feb 20 '16 at 03:36
  • my suggestion is you debug this `require_once 'assets/PHP/membership.php';` related code, most likely it is throwing a fatal error. comment it out to confirm @user2615426 . make sure you file is a PHP script as well – meda Feb 20 '16 at 03:36
  • I came across this post before posting here and tried the majority of the fixes on it. I'll go back and see if I missed anything. – user2615426 Feb 20 '16 at 03:40
  • @meda I commented out that code and am still getting the same array size 0, it must be a setting as suggested earlier. – user2615426 Feb 20 '16 at 03:44
  • If it shows 0 before u press submit thats normal because you need to check if submit is set like the answer below – meda Feb 20 '16 at 03:46
  • that statement prevents a var_dump from happening inside it because the $_POST variable is empty. – user2615426 Feb 20 '16 at 03:54
  • If you see 0 before you submit the form, you should check like the answer below, also try `var_dump($_REQUEST)` – meda Feb 20 '16 at 04:29
  • I'm seeing 0 before and after I submit the form. I'll check out the request var_dump Thanks! – user2615426 Feb 20 '16 at 04:36

2 Answers2

0

For anyone who is curious, I solved this by finding this reddit link: https://www.reddit.com/r/PHPhelp/comments/3uxapu/phpstorm_local_php_5616_post_always_empty/

All my superglobals were coming in blank. I'm not even sure why quite yet, I still need to read more about it. The following code snippet got me the results I needed:

$post_data = file_get_contents('php://input');

Post data now contains what I wanted to get from $_POST. Thanks to everyone who tried to help me here!

0

If you want print $_post thn use code this....

If(isset($_POST['submit'])){
   Print_r($_POST);
 }

It's because, if you want to print data from form then you must submit the form first then you'll be able to get data in $_post.....

Sonu Bamniya
  • 1,095
  • 1
  • 13
  • 29