Search code examples
phphtmlgoogle-search-appliance

Pass HTML input value between tabs of a search box?


I have a GSA search box with tabs (Site Search, People and Library) for different collections. I'd like the query parameter (q) to be persistent when a new tab is clicked.

Here are the HTML tabs:

<div id="hd">
    <ul id="nav">
        <li id="tab1"><a href="./index-tab.php?site=master-MSU">Site Search</a></li>
        <li id="tab2"><a href="http://www.montana.edu/search/">People</a></li>
        <li id="tab3"><a href="./index-tab.php?site=libpub-library-website">Library</a></li>
    </ul>
</div>

The search parameter is acquired this way (PHP):

    // Set default value for query
$q = isset($_GET['q']) ? trim(htmlentities(strip_tags($_GET['q']))) : null;

Here is the HTML search box:

    <form id="searchBox" method="get" action="./index-tab.php">
    <fieldset>
        <label for="q">Search</label>
        <input type="text" maxlength="200" name="q" id="q" tabindex="1" value="<?php echo (!is_null($q) ? $q : $suggestedSearch); ?>" onclick="if (this.value == '<?php echo $suggestedSearch; ?>') { this.value = ''; }" onblur="if (this.value == '') { this.value = '<?php echo $suggestedSearch; ?>'; }" />
        <input type="hidden" maxlength="200" name="site" id="site" tabindex="1" value="<?php echo (!is_null($site) ? $site : $site); ?>" onclick="if (this.value == '<?php echo $site; ?>') { this.value = ''; }" onblur="if (this.value == '') { this.value = '<?php echo $site; ?>'; }" />
        <button type="submit" class="button">Search</button>
    </fieldset>
</form>

I suspect I need to use jquery and AJAX but I am not familiar with either one. Any advice would be great. Thanks!


Solution

  • Why not just append the query variable to each tab link?

    <div id="hd">
        <ul id="nav">
            <li id="tab1"><a href="./index-tab.php?site=master-MSU<? if ( ! is_null($q)) { echo '&q=' . urlencode($q); } ?>">Site Search</a></li>
            <!-- ... -->
        </ul>
    </div>
    

    Note the urlencode() call - that's important if you're printing it in a URL.