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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
leakim971PluritechnicianCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.