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

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Locked
10010110
Translator
Translator
Posts: 224
Joined: Tue Jan 22, 2008 9:57 am

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

Post 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.
Last edited by 10010110 on Tue Mar 04, 2014 3:50 pm, edited 1 time in total.
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

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

Post by JohnnyB »

Is there a better way to do this? It doesn’t quite feel right
Does it work? Seems like a valid solution...
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
10010110
Translator
Translator
Posts: 224
Joined: Tue Jan 22, 2008 9:57 am

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

Post 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. :)
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

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

Post 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}
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
10010110
Translator
Translator
Posts: 224
Joined: Tue Jan 22, 2008 9:57 am

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

Post by 10010110 »

Thanks calguy1000. Makes total sense.
Locked

Return to “Modules/Add-Ons”