Jump to content

Echo the searched term with search results (solved)


ryanC
 Share

Recommended Posts

Hi, I've been working on a modified version of the default search template that comes with the "beginner" site profile.

When someone searches a term,  I would like to include the term the user searched for along with the results. 

For example, say someone searched the word "apples".

Right now I have: 

 if($matches->count) {

                    // we found matches
                    echo "<h2>Found $matches->count page(s) matching your query:</h2>";

                    // output navigation for them (see TIP below)
                    echo "<ul class='nav'>";

Is there a way I can have their term echoed back to them along with results? Something like:
 

Quote

 

 "Search results for apples"

 

 

 

Thanks!

Edited by ryanC
issue was solved
Link to comment
Share on other sites

Thanks Adrian, how would I enter that into my text? I am not very good at PHP yet.

                    echo "<h2>Found $matches->count page(s) matching your query:</h2>";
                    echo "selectorValue";

Obviously it's not just "echo selectorValue", but I don't know what to put in there.

Link to comment
Share on other sites

21 minutes ago, kongondo said:

Just be sure to sanitise the values you echo back directly using htmlentities

+1

If you happen to "echo" it with JavaScript (let's say you cannot use PHP to echo it into the search input box because that one has already been rendered) then you can do something like:

<?php if ($q) : ?>
    <script type="text/javascript">
        $('#my-search-query-input').val(htmlentities.decode('<?= $q ?>'));
    </script>
<?php endif; ?>

where htmlentities.decode is found in https://github.com/bb87/htmlentities

$q must be already sanitized of course as @kongondo pointed out, my JS example is about turning htmlentities back into regular characters which does not happen when injecting $q using JS.

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...