Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

(solved) Get Name from selected DropDownList element


Go to End


4 Posts   1728 Views

Avatar
Beppo98

Community Member, 19 Posts

15 March 2011 at 5:38am

Edited: 15/03/2011 5:40am

Hello,

I have another question which is - i think so - a very easy question.

Here is my code for creating a form:

function Form() {
  $SkripteAll = SkriptHelper::GetSkripte()->toDropdownMap('ID', 'Name');
		
  $fields = new FieldSet(
     new DropdownField('Skript','', $SkripteAll)
  );

  $actions = new FieldSet(
    new FormAction('doSubmit', 'Anzeigen')
  );

  $form = new Form(
    $this,
    'Form',  
    $fields, 
    $actions,  
    null
  );
return $form;
}

Now I want to know in the doSubmit method, which item the user has selected.
With

$data['Skript']

in the doSubmit method I get the ID (for example 1). But I want to know the name or much better I want to know the complete Skript-object, which I have mapped from the database into the dropdownfield.

Can anyone help me with my problem?

Best regards Beppo

Avatar
Devlin

Community Member, 344 Posts

15 March 2011 at 6:08am

public $SkripteAll;
function Form() { 
$this->SkripteAll = SkriptHelper::GetSkripte()->toDropdownMap('ID', 'Name');
}
function doSubmit($data,$form) {
$SkriptID = (int)$data['Skript'];
if( !empty($this->SkripteAll[$SkriptID]) ) $selectedSkript = $this->SkripteAll[$SkriptID];
}

Avatar
Beppo98

Community Member, 19 Posts

15 March 2011 at 8:10am

Thanks for your answer, now it works.

But I have another problem you perhaps could help me. Currently I have one dropdownfield and a button in my form. Depending on the chosen element in the dropdownfield I want to display different data in the frontend page.

At the moment I tried it to solve in this way:

public $SkriptanmeldungenAll;

function doSubmitDruckliste($data, $form) {
 //some code
 $this->SkriptanmeldungenAll = DataObject::get("Skriptanmeldung","SkriptID = '$SkriptID'");
 //...
}

function GetSkriptanmeldungen($SkriptID)
{ 
return  $this->SkriptanmeldungenAll;
}

An the template looks like this:

...
<% control GetSkriptanmeldungen %>
  <tr style='height:30px' >
    <td>$Nachname</td>
    <td>$Vorname</td>
    <td></td>	
  </tr>
<% end_control %>

But at the momentan no data are displayed.

Best regards, Beppo

Avatar
Beppo98

Community Member, 19 Posts

17 March 2011 at 12:34am

Edited: 17/03/2011 12:34am

For my last posting I created a new Thread:

Show data after submitting a form

Thanks for the help.