I'm rewriting a portion of the PM module. One change is to add a MEMBERS drop down list to the existing CONTACTS list on the "Write a new message" page.

The existing PM module uses form_ functions from common.inc for everything but the drop down lists, and I'm wondering if the working code I wrote for a MEMBERS list can be translated into a form_select function.

Here's the working code, with the XXonChangeXX XXed-out to get through the "suspicious code" filter on the Drupal forum:

  $form .= '<select id="edit-quick" name="quick2" XXonChangeXX="document.getElementById(\'edit-recipient\').value=quick2.value"><option value="" selected="selected">--'.t("Members").'--</option>';
  $result = db_query("SELECT name AS name from users ORDER BY name", $user->uid);
  while ($name = db_fetch_object($result)) 
  {
    $name = check_plain($name->name);
    $form .= "<option value='$name'>$name</option>";
  }
  $form .= '</select>';

Here's the function from common.inc:

function form_select($title, $name, $value, $options, $description = NULL, $extra = 0, $multiple = FALSE, $required = FALSE) 

I think I can figure out most of the parameters, but I don't understand how to fill the list from $result, or how to do the OnChange code.

If my modifications are of value, I'd be glad to contribute this to the project. Suggestions appreciated.

Curtis

Comments

gerd riesselmann’s picture

form_select takes an assoziative array as $options and the selected value's key as $value. AS extra you can pass an array of additional attributes, like your onChange handler

So you need the following code:

$result = db_query("SELECT name AS name from users ORDER BY name", $user->uid);
$arrOptions = array("--none--" => t("Members"));
while ($name = db_fetch_object($result)) {
  $name = check_plain($name->name);
  $arrOptions[$name] = $name;
}
$arrExtra = array("onChange" => "**your Javascript**");
$form .= form_select(t("Select Member"), "quick2", "--none--", $arrOptions, t("A description"), $arrExtra, FALSE, FALSE);

Note that the first entry now has key "--none--" and not and empty value as it was in your code.

------------------
Gerd Riesselmann
www.gerd-riesselmann.net

CLamont’s picture

Gerd:

Your suggested code worked great--thanks!--except that I cannot get the Javascript to work.

This form does not:

//  $arrExtra = array("XXonChangeXX" => "XXonChangeXX=document.getElementById(\'edit-recipient\').value=quick2.value");

Nor does this(as a test):

$arrExtra = array("XXonChangeXX" => "XXalertXX('This is my message.')");

Nor does this, similar to a form I saw used in other Drupal code:

 $form .= form_select(t("Select Member"), "quick2", "--none--", $arrOptions, t("A description"), "XXalertXX('This is my message.')", FALSE, FALSE);

(XXfunctionXX form necessary to get Drupal board to pass the code...)

Any thoughts?

Curtis

gerd riesselmann’s picture

Since I don't know about Drupal and JavaScript and I don't have access to Drupal's code up to tomorrow morning (which is in 24 hours), I can not really help at the moment.

However, you can try to seperate html and script, so you don't need the onChange attribute, like described here.

If your JavaScript is in a file, you can tell Drupal to include it in the html head section by passing the according <script> html to the function drupal_set_html_head

------------------
Gerd Riesselmann
www.gerd-riesselmann.net

CLamont’s picture

Thanks, Gerd. When I have time I'll follow up on that. For now, I just went ahead and coded the MEMBERS drop-down list like the existing CONTACTS list, which does not use the form_select--path of least resistance.

There is an SQL bug in the CONTACTS list, though, which I'll post in a new thread.

Curtis

gunyhake’s picture

my arrExtra = "xxonchange = alertxx('Hello world')"; this work fine for me.

I have a problem:

As the option is changed, is there anyway that I can trigger and sql query or a php function in my code. Note that onchange behaviour work with javascript only.

Any ideas?

AN