stebesplace
stebesplace

Reputation: 1331

Pass array values into select option fields within a form?

This is based in PHP.

Is it possible to take an array like this:

$SELECT_INDUSTRY = array("Medical" => "Specialty", "Dental" => "Specialty", "Pediatrics" => "Specialty");

And have that pass those two values into something simple like this:

<select>
    <option value="$SELECT_INDUSTRY[]">$SELECT_INDUSTRY[]</option>
</select>

Where Medical would be the value being passed, and Specialty would be the public facing text.

This is the function I'm using to build the actual select boxes:

$SELECT_INDUSTRY = array("Medical", "Dental", "Pediatrics");
$FORM_SELECT_SIZE = 'input-min';
function get_options_industry( $arr = array() ) {
global $FORM_SELECT_SIZE;
echo '<div class="control-group"><label class="control-label" for="industry">Industry</label><div class="controls"><select name="industry" id="industry" class="'.$FORM_SELECT_SIZE.'"><option value=>Select an Industry</option>';
foreach( $arr as $option ) {
    echo '<option>'.$option.'</option>';
}
echo '</select></div></div>';
}
$FORM_FIELD_INDUSTRY = $SELECT_INDUSTRY;

And this is how I'm displaying the select:

<?php get_options_industry( $FORM_FIELD_INDUSTRY ) ?>

I feel like I'm close, or at least on the right track, just can't figure out how to pull the first array value into the value field, then the second into the actual name.

The goal is to get the value being passed in the form, is different than the public facing name. I realize that the above array definition I gave may not be the correct way to do this.

SOLUTION

Thanks to a couple responses below, here is the final answer to solve my problem:

$SELECT_INDUSTRY = array("Medical" => "Medical", "Dental" => "Medical", "Pediatrics" => "Medical");
$FORM_SELECT_SIZE = 'input-min';
function get_options_industry( $arr = array() ) {
global $FORM_SELECT_SIZE;
echo '<div class="control-group"><label class="control-label" for="industry">Industry</label><div class="controls"><select name="industry" id="industry" class="'.$FORM_SELECT_SIZE.'"><option value=>Select an Industry</option>';
foreach( $arr as $key => $value ) {
    echo '<option value="'.$value.'">'.$key.'</option>';
}
    echo '</select></div></div>';
}
$FORM_FIELD_INDUSTRY = $SELECT_INDUSTRY;

I then have to make sure that my key is unique so no duplicates are erased.

Upvotes: 1

Views: 13468

Answers (3)

user8086021
user8086021

Reputation: 1

This one works fine

$SELECT_INDUSTRY = array("Medical", "Specialty", "Dental", "Specialty", "Pediatrics", "Specialty");

echo '<select>';    
foreach ($SELECT_INDUSTRY as $key => $value) {
    echo '<option value="' . $value . '">' . $key . '</option>';
}
echo '</select>';

Upvotes: 0

wdm
wdm

Reputation: 7189

Quick example...

$SELECT_INDUSTRY = array("Medical" => "Specialty", "Dental" => "Specialty", "Pediatrics" => "Specialty");

echo '<select>';    
foreach ($SELECT_INDUSTRY as $key => $value) {
    echo '<option value="' . $value . '">' . $key . '</option>';
}
echo '</select>';

You can reverse $key and $value depending on which you want displayed as the text and which you would like passed as the value. The way I displayed it in my example made the most sense to me.

Upvotes: 1

matt
matt

Reputation: 1984

How about this?

$SELECT_INDUSTRY = array("Medical" => "Specialty", "Dental" => "Specialty", "Pediatrics" => "Specialty");

.... other stuff you already have ....

foreach( $arr as $val => $option ) {
    echo '<option value="'.$val.'">'.$option.'</option>';
}

Upvotes: 3

Related Questions