1

After submitting an HTML form, I noticed that SOME empty input fields get sent to the $_POST array while others don't (they all have a name attribute). What determines what gets set and what does not?

<!DOCTYPE html>
<head><title>EXAMPLE TEST</title></head>
<body>

<form action="/test.php" method="post">
  <input type="text" name="text">
  <input type="radio" name="radio">
  <input type="checkbox" name="checkbox">
  <input type="date" name="date">
  <textarea name="textarea" rows="5"></textarea>
  <input type="submit" name="submit">

  <!-- etc... -->
</form>

<?php var_dump($_POST); ?>
</body>
</html>

Leaving all fields untouched and submitting the form, var_dump($_POST) shows:

array(3) { ["text"]=> string(0) "" ["textarea"]=> string(0) "" ["submit"]=> string(6) "Submit" }

I'm asking this because my script "was" seemingly adding the empty radio to $_POST["radio"] and my script was working fine, but now suddenly it doesn't appear in the array. I haven't modified anything in my script. But my question isn't about my script, my question is: how do I know which empty fields get set in the $_POST array, and is this always guaranteed?

Sunny
  • 2,232
  • 7
  • 24
  • 36
  • 1
    http://php.net/manual/en/function.isset.php ? – Black Sheep Apr 04 '14 at 00:47
  • 1
    your radio and checkbox is missng value attributes. This determines if `isset()` – jcobhams Apr 04 '14 at 00:48
  • possible duplicate of [Check if $\_POST exists](http://stackoverflow.com/questions/3496971/check-if-post-exists) – Anonymous Apr 04 '14 at 00:48
  • adding values doesn't change anything btw – Sunny Apr 04 '14 at 00:48
  • 1
    Radio button and Checkboxes only get sent if they contain a positive piece of information i.e. they are ticked or checked. Text is always sent even if empty, Buttons will be sent when they are pressed, i.e. if you have 2 buttons only the one that is pressed is sent so you can tell which one it was the user pressed. You should always check for the existance of fields in the $_POST/$_GET arrays before using them, using `isset()` or `array_key_exists()` whichever suits the situation and your own preferences – RiggsFolly Apr 04 '14 at 00:52
  • @Anonymous no no, not check if the key exists. But know what gets sent to the $_POST array when submitting the form. – Sunny Apr 04 '14 at 01:08
  • If you know what you want to be sent, it would be the same result. Obviously, the former could be done, but it seems rather frivolous. – Anonymous Apr 04 '14 at 01:11
  • @Anonymous I don't think you read my question carefully, either that or my English wasn't good. – Sunny Apr 04 '14 at 01:31

1 Answers1

3
  • Checkboxes and radio buttons will only be sent if they are checked.

  • Text inputs will always be sent (as long as they are not disabled), but will be empty strings if not filled in.

  • Submit buttons will be sent if they are clicked.

The browser and the standards determine this. It has nothing to do with PHP specifically.

Community
  • 1
  • 1
Brad
  • 159,648
  • 54
  • 349
  • 530