Page 1 of 1

[solved] Multiple search forms on a page -> keep IDs unique?

Posted: Sun Mar 02, 2014 7:43 pm
by 10010110
I want to put two search forms on my pages (one at the top and the other at the bottom), and while the forms’ IDs are unique in that they are numbered sequentially the IDs of the text input field is static, and thus, makes the HTML invalid because the same ID exists twice in the document.

My search template is pretty much original:

Code: Select all

{$startform}
<fieldset>
	<legend><label for="{$search_actionid}searchinput">{$searchprompt}</label></legend>
	<input type="text" id="{$search_actionid}searchinput" name="{$search_actionid}searchinput" size="20" maxlength="50" value="{$searchtext}" {$hogan}/>
	{*
	<br/>
	<input type="checkbox" name="{$search_actionid}use_or" value="1"/>
	*}
	<input name="submit" value="{$submittext}" type="submit" />
{if isset($hidden)}{$hidden}{/if}
</fieldset>
{$endform}
The “{$search_actionid}searchinput” is always the same with multiple forms and despite this not being allowed it also causes issues with the label association because the labels focus the wrong input.

I’ve currently employed a workaround in that I’m defining a variable and adding to that var for each subsequent form:

Code: Select all

{if isset($seq_num)}{assign var='seq_num' value=$seq_num+1}{else}{assign var='seq_num' value='1'}{/if}
…
…
<input type="text" id="{$search_actionid}searchinput_{$seq_num}" …/>
Is there a better way to do this? It doesn’t quite feel right.

Re: Multiple search forms on a page -> keep IDs unique?

Posted: Mon Mar 03, 2014 1:53 am
by JohnnyB
Is there a better way to do this? It doesn’t quite feel right
Does it work? Seems like a valid solution...

Re: Multiple search forms on a page -> keep IDs unique?

Posted: Tue Mar 04, 2014 3:50 pm
by 10010110
Yes, it works. I just had the feeling that this is kind of “hacky”. But well, if you say it’s a valid solution then I’m feeling better already. :)

Re: [solved] Multiple search forms on a page -> keep IDs uni

Posted: Tue Mar 04, 2014 4:59 pm
by calguy1000

Code: Select all

{if isset($seq_num)}{assign var='seq_num' value=$seq_num+1}{else}{assign var='seq_num' value='1'}{/if}
Shorter and a little cleaner:

Code: Select all

{if !isset($seq_num)}{$seq_num=0}{/if}{$seq_num=$seq_num+1}

Re: [solved] Multiple search forms on a page -> keep IDs uni

Posted: Tue Mar 04, 2014 8:00 pm
by 10010110
Thanks calguy1000. Makes total sense.