Recuperando el valor de la opción de selección en un formulario web

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

Cuando lo intento $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.

preguntado el 01 de febrero de 12 a las 22:02

This is expected behaviour. You have to specify a value for each <select><option value="company name">company name</option></select>. Otherwise the value will automatically be set to a numeric value. -

I have edited my qn. to show the code which I use to generate the select option. Could u let me know if the key values are initialized properly here? and if its fine? -

It's hard to see hot your <select> in the html is populated, but I guess either (a) doing $array[$row->data] = $row->data; or (b) fetch the array from your second snippet somewhere in your first snippet and use $value = $array[$form['submitted_tree']['company_1']['#options'][$key1]]; -

1 Respuestas

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!

Respondido 02 Feb 12, 06:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.