0

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!

Community
  • 1
  • 1
axxint
  • 3
  • 2

1 Answers1

1

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.

Kenaniah
  • 5,171
  • 24
  • 27
  • I set up my code like so: $form['myfield1'] = array( '#type' => 'select', '#title' => t('State'), '#options' => content_allowed_values($fields['myfield1']), '#default_value' => $myformname->myfield1, '#required' => FALSE, ); if (!$myformname->myfield1) $myformname->myfield1 = NULL; And my condition (and the various permutations I have tried) doesn't impact the output at all, so I'm not sure if there's something wrong in my coding, or some fundamental difference with how the ctools tool interprets or stores form variables. – axxint Nov 30 '11 at 16:36
  • I think the last line you posted should be above the first one. You're currently nullifying it after copying its value to `#default_value`. – Kenaniah Nov 30 '11 at 18:37