form select active value

Hi,

I have a select that pulls options from a table in our drupal database.

basically I want to run an if statement from the value of the select without submitting the form, I think an onchange event would do it, just unsure of how to do it?

<?php
function my_selector($default = NULL) {

  // Prepare the <select> options array 
  $options = array();
 
  // Write some SQL to get the options you desire and run thequery
  $sql = "SELECT id, code, description FROM {association_code} ORDER BY id";
  $r = db_query($sql);
 
  // Get each option and populate the options array.
  while ($row = db_fetch_array($r)) {
    $options[$row['code']] = $row['description'];
  }
 
  // Start making the FormAPI form.
  $form['my_select'] = array(
    '#type' => 'select',
    '#options' => $options,
    '#title' => t('Select your code'),
  );
 
  // Return the form to Drupal's FormAPI.
  return $form;
}

return drupal_get_form('my_selector');
?>

Open in new window

aarondarrAsked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
If you want to get or play with the selectbox best is to pass "this" only (first example)

So you get the value for example like that :

function myfunction(sb) {
    var valsb = sb.options[sb.selectedIndex].value;
    alert("current value is : " + valsb);
}

Open in new window

0
 
leakim971PluritechnicianCommented:
Hello aarondarr,

Try :

'#attributes' => array('onchange' => 'myfunction(this)')

$form['my_select'] = array(
    '#type' => 'select',
    '#options' => $options,
    '#title' => t('Select your code'),
    '#attributes' => array('onchange' => 'myfunction(this)')
  );

Open in new window

0
 
leakim971PluritechnicianCommented:
Or to get the value directly in your function


'#attributes' => array('onchange' => 'myfunction(this.value)')

Open in new window

0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
aarondarrAuthor Commented:
works great, thankyou. the code is way out of my understanding.

If not too much trouble can you make a change.

I have added a new field to the table called question
$sql = "SELECT id, code, description, question FROM {association_code} ORDER BY id";

I am wondering if you can set the all fields except id into session variables?



0
 
leakim971PluritechnicianCommented:
You're welcome!

aarondarr, you're premium service member, why not create a new question and close this one ?
I will do my best to answer you. Thanks in advance.
0
 
aarondarrAuthor Commented:
0
 
leakim971PluritechnicianCommented:
Thanks a lot for the points! I'm going to see the new question!
0
All Courses

From novice to tech pro — start learning today.