简体   繁体   中英

Retrieving value of select option in a webform

I would like to validate the company name chosen of the submitted from a select option - "Company"

When I try $form_values['submitted_tree']['company_1']; it shoes me the key instead of the string value. Eg. $form_values['submitted_tree']['company_1']; shows '1', which is the key of the selected option.

I tried this, but it still prints the key '1' instead of its value.

<?php
$_page_num = $form_state['values']['details']['page_num'];
$key1 = $form_state['values']['submitted_tree']['company_1'];
$value1 = $form['submitted_tree']['company_1']['#options'][$key1];
$key2 = $form_state['values']['submitted_tree']['company_2'];
$value2 = $form['submitted_tree']['company_2']['#options'][$key2];
if (($_page_num == 2) && ($value1 == $value2))
{
    drupal_set_message(print_r($form['submitted_tree']['company_2']['#options'][$key2]));
}
?>

The code which I use to populate the select option is

$array = array();
if ($_SESSION['major'] == 0) {
$sql = db_query("Select data from webform_submitted_data where nid = 2133 and cid = 20 and sid in (SELECT sid FROM webform_submitted_data WHERE nid = 2133 and cid =30 and no = 0) and data not in (SELECT data FROM webform_submitted_data where nid = 2134 and cid=15 group by data having COUNT(*) > 6) order by data ASC");
while($row = db_fetch_object($sql)) {
    $array[] = $row->data; 
}
}
return $array;

Could anyone help me to retrieve the actual text value displayed in select option chosen. I have tried form_state as well, but it returns the same result.

Even I had this problem some time ago. You could always run the query that you used to populate the select options again in your validation function. SInce you know the key you can find the value of the selected element as well. Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM