Jump to content

Easiest way for clients to add select options in form


onjegolders
 Share

Recommended Posts

Scratching my head here. I'm trying to find a simple way for the client to be able to add options to a select tag in a form.

Do I really have to use pages here?

I can use a textarea - convert new lines to <li> tags but that won't cut it for <option> tags.

Repeater field? If that is the best option could anyone tell me how I would output the results as <li> tags?

Ideally it would be a fieldtype where each drop-down was some sort of item that I could loop in <option> tags.

Thanks

Edit: I've come up with this code for a repeater file and it seems to be doing the trick.

<?php
echo "<ul>";
foreach ($page->subject_options as $item) {
 echo "<li>$item->subject_option</li>";
}
echo "</ul>";
?>

I would be interested though if there was a faster or 'cleaner' way of doing this.

Edited by onjegolders
Link to comment
Share on other sites

Pages are great for this. A recent new feature allows you to add pages in a page select, you can enable it in the fields input settings.

Using pages you could setup a section for the user to add pages used as options later, and even sort them in the tree. No need to even have a select page field on the page.

You could also just use that "section" and it's childs to output the options.

echo "<select name='myoptions'>";
foreach($pages->get("/myoptions/")->children() as $option){
   echo "<option value='$option->name'>$option->title</option>";
}
echo "</select>";

or if through a page select field "select_options" on page it would be

echo "<select name='myoptions'>";
foreach($pages->selet_options as $option){
   echo "<option value='$option->name'>$option->title</option>";
}
echo "</select>";

Other ways would be to have a textarea and each new line render as an option. Then the code would be like this:

in optionstext field:

Option1
Option2
Option3
echo "<select name='myoptions'>";
foreach(explode("\n",$page->optionstext) as $option){
   echo "<option value='$option'>$option</option>";
}
echo "</select>";
Link to comment
Share on other sites

Again late :)

I buit a function for the newlines solution:


function newlinesToOption($field){
 $select = "<select>";
 foreach(explode("\n", $field) as $line){
$line = trim($line);
$lowerLine = strtolower($line);
$select .= "<option value='$lowerLine'>$line</option>";
 }
 $select .= "</select>";
 return $select;
}
echo newlinesToOption($page->yourField);
Link to comment
Share on other sites

Wow thanks both of you!

Again late :)

I buit a function for the newlines solution:


function newlinesToOption($field){
 $select = "<select>";
 foreach(explode("\n", $field) as $line){
$line = trim($line);
$lowerLine = strtolower($line);
$select .= "<option value='$lowerLine'>$line</option>";
 }
 $select .= "</select>";
 return $select;
}
echo newlinesToOption($page->yourField);

Diogo, is this code that I can copy and paste into a new module?

Pages are great for this. A recent new feature allows you to add pages in a page select, you can enable it in the fields input settings.

Using pages you could setup a section for the user to add pages used as options later, and even sort them in the tree. No need to even have a select page field on the page.

You could also just use that "section" and it's childs to output the options.

echo "<select name='myoptions'>";
foreach($pages->get("/myoptions/")->children() as $option){
echo "<option value='$option->name'>$option->title</option>";
}
echo "</select>";

or if through a page select field "select_options" on page it would be

echo "<select name='myoptions'>";
foreach($pages->selet_options as $option){
echo "<option value='$option->name'>$option->title</option>";
}
echo "</select>";

Other ways would be to have a textarea and each new line render as an option. Then the code would be like this:

in optionstext field:

Option1
Option2
Option3
echo "<select name='myoptions'>";
foreach(explode("\n",$page->optionstext) as $option){
echo "<option value='$option'>$option</option>";
}
echo "</select>";

Soma, thanks, I do really want to use native PW functionality, I am just finding it really hard to get my head around the workflow in setting it all up.

Create template, add fields, disable editing of parent - I know it's like this at the beginning for everybody probably, but my head's in a spin!

There's lots of great documentation in the forums but the one thing I think is lacking is a real walkthrough - as much to see seasoned users' workflows as the code itself.

Link to comment
Share on other sites

It makes more sense to keep the select tags outside the function, so you can control the attributes

new code goes like this:

function newLinesToOptions($field){
 $options = "";
 foreach(explode("\n", $field) as $line){
  $line = trim($line);
  $lowerLine = strtolower($line);
  $options .= "\t<option value='$lowerLine'>$line</option>\n";
 }
 return $options;
}?>

<select name="name">
<?php echo newLinesToOptions($page->yourField); ?>
</select>

edit:

Diogo, is this code that I can copy and paste into a new module?

No, this is only a normal function. If you have to use it more then once, put the function on an include file and call it with this part of the code:

<select name="name">
<?php echo newLinesToOptions($page->yourField); ?>
</select>
Link to comment
Share on other sites

It makes more sense to keep the select tags outside the function, so you can control the attributes

new code goes like this:

function newLinesToOptions($field){
 $options = "";
 foreach(explode("\n", $field) as $line){
  $line = trim($line);
  $lowerLine = strtolower($line);
  $options .= "\t<option value='$lowerLine'>$line</option>\n";
 }
 return $options;
}?>

<select name="name">
<?php echo newLinesToOptions($page->yourField); ?>
</select>

edit:

No, this is only a normal function. If you have to use it more then once, put the function on an include file, and call it with this part of the code:

<select name="name">
<?php echo newLinesToOptions($page->yourField); ?>
</select>

OK thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...