Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Retaining selected Item in dropdown menu

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design
 
Thread Tools Search this Thread
  #1  
Old 01-08-2009, 11:56 AM
 
AMMoyer AMMoyer is offline
 

Senior Member
  
Join Date: Nov 2008
Posts: 150
 

Default Retaining selected Item in dropdown menu

I have a template I've written that is visible above the categories in the left menu. The template has a series of dropdown boxes. When a year is selected from the first the onchange event fires a query to fill the second combo box and the page reloads. What happens is that the value of the year is not remaining in the first box. The second box is populated, but the first box still says "Select Year". I just tried adding the {if} statement to the first dropdown, but no luck. I have also tried adding a series of selection lines that look like:

<option value="2009" {php} if (!(strcmp(2009, $year))){echo "selected";}{/php}>2009</option>

Without any success. How do I get the selected year to stick when the page reloads?

Here is the tpl code:
Code:
{capture name=menu} <form method="post" action="mmy.php" name="mmysearchform"> <select name="year" style="width: 90%;"onchange="javascript:document.mmysearchform.submit ();"> {if $year == ''} <option value="" selected="selected">Select Year</option> {elseif $year > 1950} <option value=$year selected="selected">"$year"</option> {/if} <option value="2009">2009</option> <option value="2008">2008</option> <option value="2007">2007</option> <option value="2006">2006</option> <option value="2005">2005</option> <option value="2004">2004</option> <option value="2003">2003</option> <option value="2002">2002</option> <option value="2001">2001</option> <option value="2000">2000</option> <option value="1999">1999</option> </select><br> {* {html_options name=year options=$yearid style="width: 90%;" onchange="javascript:document.mmysearchform.submit ();"}<br> *} {if $year == ''} <select name="make" style="width: 90%;" disabled="disabled"> <option value="" selected="selected">Select Make</option> {elseif $year > 1950} {html_options name=make values=$makeid output=$makename style="width: 90%;" onchange="javascript:document.mmysearchform.submit ();"}<br> {/if} {if $make == ''} <select name="model" style="width: 90%;" disabled="disabled"> <option value="" selected="selected">Select Model</option> {/if} {html_options name=model values=$modelid output=$modelname style="width: 90%;"}<br> <div id="mmy" align="center"> <input name="getparts" type="submit" value="Get Parts" /> </div> </form>
__________________
Adam
X-Cart Gold 4.4.5 - Live
X-Cart Gold 4.1.11 - Retired
X-Payments 1.0.4
CDSEO Pro
EWD Hosting

Last edited by AMMoyer : 01-08-2009 at 11:56 AM. Reason: adding code tags
Reply With Quote
  #2  
Old 01-08-2009, 11:37 PM
  Victor D's Avatar 
Victor D Victor D is offline
 

X-Adept
  
Join Date: Aug 2008
Posts: 643
 

Default Re: Retaining selected Item in dropdown menu

HTML Code:
<option value=$year selected="selected">"$year"</option>
More correct
HTML Code:
<option value="{$year}" selected="selected">{$year}</option>


Also it may be caused by duplicating options with the same value
F.e. if 2009 is selected you will get
HTML Code:
<option value="2009" selected="selected">2009</option> <option value="2009">2009</option>
Try to use foreach or section cycle to output options
__________________
Regards,
Victor Dubiniuk

X-Cart Skins Store
- twenty two different skins for 4.1.x and 4.2.x;
- 4.3.x compatible versions are available;
- refresh you store now!

Smart menu X-Cart add-on for 4.1.8 - 4.3.x
Featured Products Slide Show X-Cart add-on for 4.1.8 - 4.3.x
Reply With Quote
  #3  
Old 01-09-2009, 06:31 AM
 
AMMoyer AMMoyer is offline
 

Senior Member
  
Join Date: Nov 2008
Posts: 150
 

Default Re: Retaining selected Item in dropdown menu

My first attempt was without the {if} statements in the dropdown select lines and it won't retain the value after its picked. Is this because its loading a new page when the year is picked and the form is reset?

I have not tried the {foreach} or {section} method yet. I have tried the Smarty html_options statement that is commented out below the "hardcoded" select I'm working on now. It is filled from a db query in the php file and will not fill the combo box until I go to that php file in the browser, while the hardcoded way, and probably the {foreach} and {section} way load whenever the template is called.

What is the most correct way to get the Year to stick after there has been a value selected? Ideally I'd like it to remain whenever that template is called.

Thanks.
__________________
Adam
X-Cart Gold 4.4.5 - Live
X-Cart Gold 4.1.11 - Retired
X-Payments 1.0.4
CDSEO Pro
EWD Hosting
Reply With Quote
  #4  
Old 01-09-2009, 06:43 AM
  Victor D's Avatar 
Victor D Victor D is offline
 

X-Adept
  
Join Date: Aug 2008
Posts: 643
 

Default Re: Retaining selected Item in dropdown menu

Don't you forget to pass this variable to smarty in php code?
Something like this:
Code:
$smarty->assign("year", $year);
__________________
Regards,
Victor Dubiniuk

X-Cart Skins Store
- twenty two different skins for 4.1.x and 4.2.x;
- 4.3.x compatible versions are available;
- refresh you store now!

Smart menu X-Cart add-on for 4.1.8 - 4.3.x
Featured Products Slide Show X-Cart add-on for 4.1.8 - 4.3.x
Reply With Quote
  #5  
Old 01-09-2009, 07:00 AM
 
AMMoyer AMMoyer is offline
 

Senior Member
  
Join Date: Nov 2008
Posts: 150
 

Default Re: Retaining selected Item in dropdown menu

Here is my php file I probably should have posted initially. I do post the variable as "yearid" and maybe that is my issue.

Code:
<?php require "./auth.php"; require $xcart_dir."/include/categories.php"; if ($active_modules["Bestsellers"]) include $xcart_dir."/modules/Bestsellers/bestsellers.php"; $query1 = sprintf("SELECT DISTINCT yearid FROM iemmy ORDER BY yearid DESC"); $result1 = @mysql_query($query1); while ($yearrow = mysql_fetch_assoc($result1)) { $yearid[] = $yearrow['yearid']; } $smarty->assign("yearid", $yearid); $year = $_POST['year']; if ($year){ $query2 = sprintf("SELECT DISTINCT makeid, makename FROM iemmy WHERE yearid='$year'"); $result2 = @mysql_query($query2); while ($makerow = mysql_fetch_assoc($result2)) { $makeid[] = $makerow['makeid']; $makename[] = $makerow['makename']; } } $smarty->assign("makeid", $makeid); $smarty->assign("makename", $makename); $make = $_POST['make']; if ($make){ $query3 = sprintf("SELECT DISTINCT modelid, modelname FROM iemmy WHERE makeid='$make' AND yearid='$year'"); $result3 = @mysql_query($query3); while ($modelrow = mysql_fetch_assoc($result3)) { $modelid[] = $modelrow['modelid']; $modelname[] = $modelrow['modelname']; } } $smarty->assign("modelid", $modelid); $smarty->assign("modelname", $modelname); $smarty->assign("main", "pages"); $smarty->assign("location", $location); func_display("customer/home.tpl",$smarty); ?>
__________________
Adam
X-Cart Gold 4.4.5 - Live
X-Cart Gold 4.1.11 - Retired
X-Payments 1.0.4
CDSEO Pro
EWD Hosting
Reply With Quote
  #6  
Old 01-09-2009, 07:22 AM
  Victor D's Avatar 
Victor D Victor D is offline
 

X-Adept
  
Join Date: Aug 2008
Posts: 643
 

Default Re: Retaining selected Item in dropdown menu

Yes
$smarty->assign("yearid", $yearid);

should be
$smarty->assign("year", $yearid);

Also all the variables that came with POST are accesible directly:
Code:
$make = $_POST['make']; if ($make){

The first line is excessible - top.inc.php do this job for you
__________________
Regards,
Victor Dubiniuk

X-Cart Skins Store
- twenty two different skins for 4.1.x and 4.2.x;
- 4.3.x compatible versions are available;
- refresh you store now!

Smart menu X-Cart add-on for 4.1.8 - 4.3.x
Featured Products Slide Show X-Cart add-on for 4.1.8 - 4.3.x
Reply With Quote
  #7  
Old 01-09-2009, 07:49 AM
 
AMMoyer AMMoyer is offline
 

Senior Member
  
Join Date: Nov 2008
Posts: 150
 

Default Re: Retaining selected Item in dropdown menu

Thank you Victor D!! I changed

$smarty->assign("yearid", $yearid);
to
$smarty->assign("year", $yearid);
but this does not help with the code below.

Now here is my tpl file that I'm trying to work with the {foreach} statement and it only shows "Array" for all choices in the combo box. I am obviously missing something.

Code:
{capture name=menu} <form method="post" action="mmy.php" name="mmysearchform"> <select name="year" style="width: 90%;"onchange="javascript:document.mmysearchform.submit ();"> <option value="" selected="selected">Select Year</option> {foreach from=$yearid item=y} <option value="{$yearid}"{if $y eq $yearid} selected="selected"{/if}>{$yearid}</option> {/foreach} {html_options name=make values=$makeid output=$makename style="width: 90%;" onchange="javascript:document.mmysearchform.submit ();"}<br> {html_options name=model values=$modelid output=$modelname style="width: 90%;"}<br> <div id="mmy" align="center"> <input name="getparts" type="submit" value="Get Parts" /> </div> </form> {/capture} { include file="menu.tpl" dingbats="dingbats_categorie.gif" menu_title=$lng.lbl_mmy_search menu_content=$smarty.capture.menu cellpadding=$fc_cellpadding}
__________________
Adam
X-Cart Gold 4.4.5 - Live
X-Cart Gold 4.1.11 - Retired
X-Payments 1.0.4
CDSEO Pro
EWD Hosting
Reply With Quote
  #8  
Old 01-12-2009, 12:04 AM
  Victor D's Avatar 
Victor D Victor D is offline
 

X-Adept
  
Join Date: Aug 2008
Posts: 643
 

Default Re: Retaining selected Item in dropdown menu

Ooops.
Quote:
I do post the variable as "yearid" and maybe that is my issue.
You should pass to Smarty year, not yearid as you POST it as year.


so
$smarty->assign("year", $year);
should work
__________________
Regards,
Victor Dubiniuk

X-Cart Skins Store
- twenty two different skins for 4.1.x and 4.2.x;
- 4.3.x compatible versions are available;
- refresh you store now!

Smart menu X-Cart add-on for 4.1.8 - 4.3.x
Featured Products Slide Show X-Cart add-on for 4.1.8 - 4.3.x
Reply With Quote
  #9  
Old 01-12-2009, 09:46 AM
 
AMMoyer AMMoyer is offline
 

Senior Member
  
Join Date: Nov 2008
Posts: 150
 

Default Re: Retaining selected Item in dropdown menu

Thank you Victor!!!

One last question on this subject. The years are working great and then the Makes are loading properly, but I am having trouble getting the Make to "stick" in the dropdown box also. I'm sure there is something mis-named or there's an issue with the {if} statement, but I've been staring at it too long to find it. Thanks again.

Partial TPL code:
Code:
<select name="make" style="width: 90%;" onchange="javascript:document.mmysearchform.submit ();"{if $year == ''} disabled="disabled"{/if}> <option value="" selected="selected">Select Make</option> {foreach item=make from=$makes} <option value="{$make.makeid}"{if $make.makeid == $make} selected="selected"{/if}>{$make.makename}</option> {/foreach} </select><br>

Partial PHP code:
Code:
$year = $_POST['year']; if ($year){ $query2 = sprintf("SELECT DISTINCT makeid, makename FROM iemmy WHERE yearid='$year'"); $result2 = @mysql_query($query2); while ($makerow = mysql_fetch_assoc($result2)) { $makelist['makeid'] = $makerow['makeid']; $makelist['makename'] = $makerow['makename']; $makes[] = $makelist; } } $smarty->assign("makes", $makes);
__________________
Adam
X-Cart Gold 4.4.5 - Live
X-Cart Gold 4.1.11 - Retired
X-Payments 1.0.4
CDSEO Pro
EWD Hosting
Reply With Quote
  #10  
Old 01-13-2009, 12:15 AM
  Victor D's Avatar 
Victor D Victor D is offline
 

X-Adept
  
Join Date: Aug 2008
Posts: 643
 

Default Re: Retaining selected Item in dropdown menu

You didn't pass the active make to template once again.
The code you have posted is not enough so I used sources posted above

insert below the line
$smarty->assign("makes", $makes);
this one
Code:
$smarty->assign("make", $make);

and in tpl:
Code:
<select name="make" style="width: 90%;" onchange="javascript:document.mmysearchform.submit ();"{if $year == ''} disabled="disabled"{/if}> <option value="" {if $make ne ''}selected="selected"{/if}>Select Make</option> {foreach item=make from=$makes} <option value="{$make.makeid}"{if $make.makeid eq $make} selected="selected"{/if}>{$make.makename}</option> {/foreach} </select>

I used to take the sheet of paper and make a draft when something is unclear.
__________________
Regards,
Victor Dubiniuk

X-Cart Skins Store
- twenty two different skins for 4.1.x and 4.2.x;
- 4.3.x compatible versions are available;
- refresh you store now!

Smart menu X-Cart add-on for 4.1.8 - 4.3.x
Featured Products Slide Show X-Cart add-on for 4.1.8 - 4.3.x
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 09:08 AM.

   

 
X-Cart forums © 2001-2020