I'm building forms for Drupal 6 through the cTools wizard (which is primarily php), and I have a few select boxes that are optional. I have my options set up like so:
0|Select
1|option 1
2|option 2
3|option 3
4|option 4
Or on some, like a year or state select, I'm using array_unshift to append a "Select" to the front of a list that is built dynamically or referenced from elsewhere.
When there is no user input, these select boxes return values of "Select", 0, or -1 when I want them to return nothing at all.
I'm pretty new to PHP, so I'm probably not asking this in the right way because I haven't found anything at all about what seems like it should be a fairly common issue. Anyone have any idea how do to this, or how to better phrase my issue to find answers? Thanks!
Because the field gets posted, you will always receive a string value of some sort in $_POST
. I normally provide a blank value (<option value="">Select</option>
) which gives me a blank string on the PHP side. You can then (if you don't mind casting "0" -> false) do something like
<?php if(!$_POST['field']) $_POST['field'] = null; ?>
to null out that value.