1

I am checking $_POST arrays, but I cannot find a way to check whether the array is empty. Does anyone know how I can check this. I have tried !empty() and isset(), but both ways returning true.

My HTML form with a select box, text field and submit button:

<form method="post">
    <select name="product_name[]">
      <option value="Camera">Camera</option>
      <option value="Radio">Radio</option>
      <option value="Television">Television</option>
    </select>
    <input name="product_price[]" />

    <input type="submit" name="submit">
</form> 

My PHP:

if( ! empty( $_POST['product_name'] ) && ! empty( $_POST['product_price'] ) ) {

    print_r( $_POST['product_name'] );
    print_r( $_POST['product_price'] );
}

Return:

Array ( [0] => Camera ) 
Array ( [0] => )
Robbert
  • 1,270
  • 6
  • 30
  • 62

4 Answers4

3

Its an array so you need to use array_filter to get rid of empty strings.

if(!empty($_POST['product_name']) && array_filter($_POST['product_price'])){    
    print_r( $_POST['product_name']);
    print_r( $_POST['product_price']);
}

Check here.

Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • 2
    Please don't use `empty(array_filter())`, just `array_filter` will do just fine. In fact, your code throws errors in PHP < 5.5. – deceze Dec 12 '14 at 11:46
1

Maybe You should just add default first option as empty string? And don't name inputs like product_name[]. Change it to product_name.

<form method="post">
    <select name="product_name">
        <option value="">-</option>
        <option value="Camera">Camera</option>
        <option value="Radio">Radio</option>
        <option value="Television">Television</option>
    </select>
    <input name="product_price" type="text" />

    <input type="submit" name="submit">
</form>

If that first option will be chosen, empty() function should return TRUE.

Jazi
  • 6,569
  • 13
  • 60
  • 92
  • if you change the statement of the select from `product_name[]` to `product_name` it will not return an array anymore – Alex Dec 12 '14 at 11:45
  • 1
    And for what You need `input` type `text` as array? And... if You need `select` to have multiple values, You just simply add `multiple` attribute (http://www.w3schools.com/tags/att_select_multiple.asp). – Jazi Dec 12 '14 at 11:47
0

You have declared product_name[](hence array empty will return TRUE if option 1 is selected) you would have to change it to product_name

<form method="post">
    <select name="product_name">
      <option value="">-</option>
      <option value="Camera">Camera</option>
      <option value="Radio">Radio</option>
      <option value="Television">Television</option>
    </select>
    <input name="product_price" />

    <input type="submit" name="submit">
</form> 
-2

Ok, I found the solution. Previous answer was wrong hope this will help you:

<form method="post">
    <select name="product_name[]">
      <option value="Camera">Camera</option>
      <option value="Radio">Radio</option>
      <option value="Television">Television</option>
    </select>
    <input name="product_price[]" />

    <input type="submit" name="submit">
</form> 

<?php

if( !empty( $_POST['product_name'] ) && !count( array_filter($_POST['product_price'])) ==0 ) {

    print_r( $_POST['product_name'] );
    print_r( $_POST['product_price'] );
}
?>
Manoj Sharma
  • 596
  • 1
  • 6
  • 23