View previous topic :: View next topic |
Author |
Message |
nprioleau Smarty Rookie
Joined: 15 Feb 2016 Posts: 7
|
Posted: Mon Feb 15, 2016 8:57 am Post subject: Select Boxes and Radio Button Value Setting |
|
|
I am trying to set the value of a radio button and selectbox with values from the DB. I have retrieved my array from the DB and have the following code.
Code: | <select name="cont_contactGroup">
<option value="1">Customer</option>
<option value="2">Supplier</option>
{html_options selected=$contact.cont_contactGroup}
</select>
|
This does not work. The value of $contact.cont_contactGroup = 1. Please assist.[/code] |
|
Back to top |
|
CrazyTemplate Smarty Rookie
Joined: 12 Sep 2011 Posts: 31
|
Posted: Mon Feb 15, 2016 2:42 pm Post subject: |
|
|
I believe that the {html_options} function creates the ENTIRE select tag, so what you write would generate:
<select>... <select></select></select>
which is broken html.
I think you need to read the docs: http://www.smarty.net/docsv2/en/language.function.html.options.tpl
So in your case you'd write something like:
Code: |
{html_options name="myselect" values=$yourArray selected=$yourSelectedId}
|
where $yourArray will contain {1=>"Customer", 2=>"Supplier"} and $yourSelectedId will be "1" to select the customer.
Last edited by CrazyTemplate on Mon Feb 15, 2016 7:28 pm; edited 1 time in total |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Feb 15, 2016 5:40 pm Post subject: |
|
|
{html_options} only create <option> tags to the best of my knowledge. |
|
Back to top |
|
CrazyTemplate Smarty Rookie
Joined: 12 Sep 2011 Posts: 31
|
Posted: Mon Feb 15, 2016 7:29 pm Post subject: |
|
|
AnrDaemon wrote: | {html_options} only create <option> tags to the best of my knowledge. |
Apparently not. When the "name=" argument is used, it generates a full select.
Quote from the docs:
Quote: | The following template will generate a drop-down list. Note the presence of the name attribute which creates the <select> tags. |
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Feb 15, 2016 9:36 pm Post subject: |
|
|
You need to pass html_options the array of options.
Code: | {html_options options=$myOptions selected=$mySelect} |
|
|
Back to top |
|
nprioleau Smarty Rookie
Joined: 15 Feb 2016 Posts: 7
|
Posted: Tue Feb 16, 2016 6:07 am Post subject: This is what I have now |
|
|
I have this code now.
Code: | // Get Contact Groups
$sql = $db->query(sprintf("SELECT ID, Title FROM contact_groups")) or SQLError();
while($contactgroup = $sql->fetch_assoc()) {
$contgrp[] = $contactgroup;
$contname[$contactgroup['Title']] = $contactgroup['Title'];
$contid[$contactgroup['ID']] = $contactgroup['ID'];
}
$smarty->assign('contgrp', $contgrp);
$smarty->assign('contname', $contname);
$smarty->assign('contid', $contid);
print_r(array_values($contgrp)); (Result Array ( [0] => Array ( [ID] => 1 [Title] => Customers ) [1] => Array ( [ID] => 2 [Title] => Suppliers ) )
and this for template:
<select name="cont_contactGroup">
{html_options options=$contgrp selected=$contid}
</select>
|
My select list is very weird, I think I have something wrong.
[/img] |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Feb 16, 2016 9:40 pm Post subject: |
|
|
the options parameter expects the values to be key=>value based. Check the data being passed in. It looks like you are passing in arrays of arrays. |
|
Back to top |
|
nprioleau Smarty Rookie
Joined: 15 Feb 2016 Posts: 7
|
Posted: Wed Feb 17, 2016 7:26 am Post subject: The fix that works but is not html_options |
|
|
So after scouring the internet I have found this code that works that doesn't use {hmtl_options} Try as I may, I cannot get the same result from {html_options}
Code: | <select name="cont_contactGroup">
{foreach from=$contgrp item=grp}
<option value="{$grp.ID}" {if $grp.ID eq $contact.cont_contactGroup} selected="selected"{/if}>{$grp.Title}
{/foreach}
</select> |
|
|
Back to top |
|
|