Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Select Boxes and Radio Button Value Setting

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
nprioleau
Smarty Rookie


Joined: 15 Feb 2016
Posts: 7

PostPosted: Mon Feb 15, 2016 8:57 am    Post subject: Select Boxes and Radio Button Value Setting Reply with quote

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
View user's profile Send private message
CrazyTemplate
Smarty Rookie


Joined: 12 Sep 2011
Posts: 31

PostPosted: Mon Feb 15, 2016 2:42 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Feb 15, 2016 5:40 pm    Post subject: Reply with quote

{html_options} only create <option> tags to the best of my knowledge.
Back to top
View user's profile Send private message
CrazyTemplate
Smarty Rookie


Joined: 12 Sep 2011
Posts: 31

PostPosted: Mon Feb 15, 2016 7:29 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Feb 15, 2016 9:36 pm    Post subject: Reply with quote

You need to pass html_options the array of options.

Code:
{html_options options=$myOptions selected=$mySelect}
Back to top
View user's profile Send private message Visit poster's website
nprioleau
Smarty Rookie


Joined: 15 Feb 2016
Posts: 7

PostPosted: Tue Feb 16, 2016 6:07 am    Post subject: This is what I have now Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Feb 16, 2016 9:40 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
nprioleau
Smarty Rookie


Joined: 15 Feb 2016
Posts: 7

PostPosted: Wed Feb 17, 2016 7:26 am    Post subject: The fix that works but is not html_options Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP