Forums

Set first option from dynamic list

Theon 07 Feb, 2015
Hello,

I'm using a multiple dropdown for my form.
At the third dropdown there's the following custom code:
<?php
$options = array();
if ( !$form->data['Data'] || count($form->data['Data']) < 1 ) {
  // no result was found
  $options[] = '0';
} 
else {
           foreach ( $form->data['Data'] as $d ) 
                 {
                 $options[$d['naam']] =  $d['tm'];
                 }
         }
echo json_encode($options);
?>

Now here's my struggle,

If the first option is the right one, nobody clicks on it.
And after form submit, the value isn't selected, so it won't save that value in my database.

What can i do about that.

Theon
GreyHead 09 Feb, 2015
Hi Theon,

Well found , I think we need to update the FAQ, Please try this version of the code.
<?php
$options = array();

if ( !$form->data['Data'] || count($form->data['Data']) < 1 ) {
  // no result was found
 $options[] = 'Please choose a . . .';
} 
else {
  $options[] = 'Please choose';
           foreach ( $form->data['Data'] as $d ) 
                 {
                 $options[$d['naam']] =  $d['tm'];
                 }
         }
echo json_encode($options);
?>
That should make 'Please choose' the first option in the list.

Bob
Theon 09 Feb, 2015
Hello Bob,

I followed your suggestion, but now i've got a new problem with the json_encode.
The selected value should be an Integer, I want to calculate with that.
The calculated fields now show 'Nan'. How do i split 'field:value' to value alone?

Greetz
Theon
GreyHead 10 Feb, 2015
Hi Theon,

I'm not a JavaScript expert but it seems to me that mot variables are treated as strings unless you specify them as numbers. For integers parseInt(var) seems to work for me.

I'm not clear what you are asking here "How do i split 'field:value' to value alone?" ??

Bob
Theon 11 Feb, 2015
1 Likes
Hello Bob,

Apparently i shouldn't be doing this kind of stuff a 2:30 anymore 😲

When i changed $options[$d['naam']] = $d['tm']; ( like John=28)
to $options[$d['tm']] = $d['tm']; ( like 28=28)

It started to work fine.
In my code i was asking for "John*28" , no wonder it said NaN.

I still have another question, but i guess i should post that one in a new topic, since it has got to do with a DB Update.

Thanks for now,

Theon
This topic is locked and no more replies can be posted.