lapointe
lapointe

Reputation: 1

php how to display ALL array items including null value items

I try to list ALL $_POST array items using var_dump (or echo), but null value items are not displayed. If I use var_dump($_POST) null doesn't appears, but if I use var_dump($_Post["nullitem"]) null appears:

<html>
    <head>
    </head>
    <body>
        <?php
        if  ($_POST["submit"]){
            var_dump($_POST);           
            foreach ($_POST as $key => $value) {
                echo $key."=>";
                echo $value;
                echo "     -     ";
            }
            echo "<br>";
            echo "ck_1 "; var_dump($_POST["ck_1"]);
            echo "ck_2 "; var_dump($_POST["ck_2"]);
            echo "ck_3 "; var_dump($_POST["ck_3"]);     
        }
?>
    <form action='test.php' method='post'  name='form_example' id='test'>
        <label for='ck_1'>
        <input type='checkbox' value=1 id='ck_1' name='ck_1'  />
        1 </label>
        <label for='ck_2'>
        <input type='checkbox' value=1 id='ck_2' name='ck_2'  checked='checked'   />
        2 </label>
        <label for='ck_3'>
        <input type='checkbox' value=1 id='ck_3' name='ck_3'  />
        3 </label>
        <input type='submit' name='submit' value='Submit'  />
    </form>
    </body>
</html>

Only ck_2 is checked, so this example will output :

array 'ck_2' => string '1' (length=1) 'submit' => string 'Submit' (length=6)

ck_2=>1 - submit=>Submit -

ck_1 null ck_2 string '1' (length=1) ck_3 null

How can I include ALL $_POST values in foreach loop (I don't know how many keys nor names in $_POST array) Thanks for help Regards

Sorry. The unchecked checkbox is not set, so is not member of $_POST array and does not appears A way to get a value for unchecked checkbox is to set an hidden field with same name and id and unchecked value (like 0), so at post time if unchecked hidden value is returned :

<input type="hidden" name="cx1" value="0" />
<input type="checkbox" name="cx1" value="1" />

Thank's Midzai

Upvotes: 0

Views: 1107

Answers (2)

Sibiraj PR
Sibiraj PR

Reputation: 1481

Try this

<html>
  <head>
  </head>
  <body>
    <?php
    if  ($_POST["submit"]){
       echo "<pre>";            
       print_r(array_filter($_POST["ck_1"]));
       echo "</pre>";
    }
   ?>
  <form action='test.php' method='post'  name='form_example' id='test'>
    <label for='ck_1'>
    <input type='checkbox' value=1 id='ck_1' name='ck_1[]'  />
    1 </label>
    <label for='ck_2'>
    <input type='checkbox' value=1 id='ck_2' name='ck_2[]'  checked='checked'   />
    2 </label>
    <label for='ck_3'>
    <input type='checkbox' value=1 id='ck_3' name='ck_3[]'  />
    3 </label>
    <input type='submit' name='submit' value='Submit'  />
  </form>
 </body>
</html>

Upvotes: 0

Martina
Martina

Reputation: 1672

I think you are including all the values of $_POST array in your foreach. The thing is, if you don't check in the checkbox the $_POST array won't contain it's key nor it's value.

checkbox i believe has only one value possible and that shows only when you "check-in" the checkbox. othervise the $_POST isn't populated with the key. Why you see NULL when you direcly query the $_POST with the specified key name (name of the checkbox that wasn't set) the key does not exist in the $_POST array and to return something it returns NULL.

If you for some obscure reason need to list all the checkboxes that were available to be chcecked in to the user, you can add

<input type='hidden' name='cbNames[]' value='ck_1'/>
<input type='hidden' name='cbNames[]' value='ck_2'/>
<input type='hidden' name='cbNames[]' value='ck_3'/>

for each of the checkboxes on your site and then list through the $_POST['cbNames'] array and query the $_POST for those:

foreach ($_POST['cbNames'] as $cbName)
    print $_POST[$cbName];

Upvotes: 0

Related Questions