oliboon
oliboon

Reputation: 361

Passing array as value of option in form

I'm currently trying to send an array as the value of an option in a select but I cannot access the data after the form is sent.

echo '<select id="domainResults_domaines" name="domainResults_domaines">';
foreach($domainsFound as $domain){
    echo '<option value="'.$domain.'">'.$domain['domaine'].'</option>';
}
echo '</select>';

The $domain array contains 2 values and I want to be able to access both of those after sending the form. Is there a way to send the array as the value or is there another way to pass 2 variables in a single option?

Thanks for the help.

Upvotes: 0

Views: 2267

Answers (2)

axel.michel
axel.michel

Reputation: 5764

Value has to be string, you could use something like json_encode to set the value and json_decode to get the array back on server side.

foreach($domainsFound as $domain){
    echo '<option value="'.htmlspecialchars(json_encode($domain)).'">'.$domain['domaine'].'</option>';
}

Upvotes: 1

SeanWM
SeanWM

Reputation: 16989

Try:

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

Check out PHP foreach.

Upvotes: 1

Related Questions