Adding hidden parameters to search-form template

I’m trying to make different search-form.phtml files in my theme/view/common/block-layout directory to allow for more specific searches on individual pages.

Below is my search-form code, mostly taken from my theme with the added “hidden” input type below. Currently, if I add a normal advanced query ("?fulltext_search=&property…"), it will change the resulting URL but will not change the advanced search parameters at all. Instead of seeing one author’s “letters” for example, the user sees every author’s letters.

Is there some other section I should be changing? I just want to add two hidden parameters with an OR function to whatever the user types into the search box.

<?php
$translate = $this->plugin('translate');
$searchValue = $this->escapeHtml($this->params()->fromQuery('fulltext_search', ''));
?>

<form action="<?php echo $this->escapeHtml($this->url('site/resource', ['controller' => 'item','action' => 'browse'], true)); ?>" id="" class="input-group">
    <input type="text" name="fulltext_search" class="input-group-field" value="<?php echo $searchValue; ?>" placeholder="<?php echo $translate('Search'); ?>">
    <input type="hidden" name="author" value="QUERY CURRENTLY HERE">
    <div class="input-group-button">
        <button class="button" type="submit"><?php echo $translate('Search'); ?></button>
    </div>
</form>

The problem here, I think, is just the name of your hidden input. author isn’t a key that has any significance to the search, so adding it in a hidden input won’t have any effect (unless you have a module or something that does use that key, but since you didn’t mention anything like that, I assume that’s not what’s happening).

The simplest thing to do is use the advanced search to compose a query that limits the search in the way you’re looking for, then that URL will contain the keys and values that you’d need to use to have the same effect as hidden inputs.

1 Like

Reading more closely, it looks like you’re already doing the “do an advanced search and use that URL” part, but you tried to include that whole query in a single hidden input? Is that right?

If so, the advice is still the same but the specific misunderstanding you’re running into is just on how hidden inputs work: each is just one “entry” in the query string, one something=something pair. So you need a separate hidden input for each piece of your query that you want to apply. Taking a random example of an advanced search for a single value of a single property, the relevant part of the query is:

property[0][joiner]=and&property[0][property]=16&property[0][type]=eq&property[0][text]=something`

And to “bake that in” to a search form with hidden inputs would look like:

<input type="hidden" name="property[0][joiner]" value="and">
<input type="hidden" name="property[0][property]" value="16">
<input type="hidden" name="property[0][type]" value="eq">
<input type="hidden" name="property[0][text]" value="something">

If I’m understanding you correctly and you want to do this with a value query with two “rows,” you’d be looking at 8 hidden inputs (you could get away with 7, the “joiner” for the first entry isn’t actually necessary, but still).

1 Like

That’s exactly what I’m trying to do - thank you! Took a few tries to figure out the parameters, but it’s working now.

This topic was automatically closed 250 days after the last reply. New replies are no longer allowed.