Viewed   196 times

so I'm trying to simply send one field of data from a form to a php file. Below is my form in a table. I also posted my php code. It keeps returning that $username is null. Ive tried post/get and it doesn't seem to matter.


<form action='' method='POST'>
            <td>First Name:</td>
            <td><input type='text' id='first'></td>
            <td>Last Name:</td>
            <td><input type='text' id='last'></td>
            <td><input type='text' id='email'></td>
            <td>Minecraft Name:</td>
            <td><input type='text' name='user'></td>
            <td><input type='submit' value='Send'></td>
            <td><input type='reset' value='Reset'></td>


if (isset($_POST['user'])) {
    $username = $_POST['user'];
    echo $username;
    echo 'username is not null';



This code is working. You need to add some condition, that checks, if $username is posted or not.

Something like that:

    $username ='';
        $username = $_POST['user'];
    if ($username==null || !$username)
         echo 'username is null';
     echo strlen($username);
     echo $username;

Thursday, August 18, 2022

The good news is that PHP and JavaScript have a similar idea about what values are true and false.

  • An empty string will be false on both sides. A string with something in it (except 0 in PHP) will be true on both sides.
  • The number 0 will be false on both sides. All other numbers will be true on both sides.

Since the values of a form will always be strings, as Quentin pointed out in his answer, a good practice might be to use an empty string as false value and something else (e.g. 'true') as true value. But I think your way of using 0 and 1 and testing the numerical values is the safest approach because it isn't misleading. (When someone sees 'true' they might think 'false' would also be usable for a false value.

Saturday, August 20, 2022

We can divide form submissions in three cases:

  1. Submissions with content type application/x-www-form-urlencoded
  2. Submissions with content type multipart/form-data
  3. Other submissions.

In cases 1 and 3, $HTTP_RAW_POST_DATA contains the raw post data (except if the option is always_populate_raw_post_data is set to false, in which case $HTTP_RAW_POST_DATA is empty in case 1), i.e., the data exactly as the client (usually the browser) has sent it. In case, 1, the data has a form such as


PHP automatically parses this, so that $_POST becomes:

$_POST = array(
    "key1" => "value1",
    "key2" => "value2",
    "key3" => array("value3.1", "value3.2");

The contents of the raw data can also be access through php://input, even in case 1 when always_populate_raw_post_data is set to false. In particular, file_get_contents("php://input") gives the same data $HTTP_RAW_POST_DATA has or would have.

In case 3, in which the POST data is arbitrary, $_POST will be an empty array and $HTTP_RAW_POST_DATA will always be populated.

Case 2 is a special one. In that case, PHP will parse the data and $_POST will get the content of the fields which are not uploaded files, but php://input and $HTTP_RAW_POST_DATA will be unavailable.

Monday, November 14, 2022

None of your inputs have name attribute

Therefore, nothing sent.

        <input type="password" id="password" placeholder="Password">

should be:

        <input type="password" id="password" name="password" placeholder="Password">

ID is just for DOM

Tuesday, December 13, 2022

No, you can't (although it seems pointless to know it).

Once you set an array-type form field, it yields as much indexes as there are values to this. In that case, it's an empty string, meaning its value was nothing (''), just because it exists. To PHP, having no explicit value means being something null or empty, which counts as valid value. Nothing means not being declared or defined at all, which is not the case of an empty array, or an array of empty value(s).

<form method="POST" id="test" action="test.php">
	<input type='hidden' name='items[]' />
<input type="submit" value="Send" form="test">

OUTPUT of print_r($_POST)

    [items] => Array
            [0] => 


If you want $_POST to be an empty array, just don't send anything via POST method. The superglobal is set at runtime as an empty array. Have fun. :)

Saturday, September 24, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :