Using a gif to search instead of a search box

Not sure how to explain this but I’ll try.

I have a standard search box which passes a ‘term’ to the search results page.

Is it possible to use an image, so when a user clicks the image the browser goes to the search page and searches for a term attached to the image?

I’m thinking using an image surrounded by form tags?

Phil

Something not quite right here, it’s not passing the term ‘swing’ to the search page.

<form action=“cat/searchresults.php” method=“post”>
<input type=“image” onclick=“submit();” src=“…/images/limage.jpg” name=“term” value=“swing” ></form>

pass the value using a hidden form field, rather than trying to attach it to the image?

Well done, good answer, all fixed

&lt;form action="cat/searchresults.php" method="post"&gt;
&lt;input name="" input type="image" onclick="submit();" src="../images/swings.jpg" /&gt;
&lt;input name="term" type="hidden" value="swing" /&gt;
&lt;/form&gt;

You could use the GET method:

<a href=“cat/searchresults.php?term=swing”><img src=“cat.gif” /></a>

searchresults.php


var_dump($_GET['term']);

// swing

Then searches can be bookmarked and shared too. Downside: the bots will hit your search page nightly unless you take some kind of blocking action in robots.txt or similar.

OT - ahh, ok - I got the wrong end of the stick then

Now I need to get it working on plain text html links :slight_smile:

I’ve used your

<a href=“cat/searchresults.php?term=swing”>Swing</a>

But the searchresults.php isn’t picking up the term.


var_dump($_GET['term']); 

If you post that line at the top of the searching action page what does it give then? (after you click the link)

I’m picking up the search term with

var_dump($_GET['term']); 
$term = $_POST['term'];

If I use the html link I’m trying I get

string(0) “”

But the search result pulls everything from the database

When I search using the normal search box I get

NULL

And the search works for the search term I entered in the search box

Create a single file with this in it for me, load it and click the link – can you? Am I going nuts?

test.php


<?php
if(isset($_GET)){
var_dump($_GET);
}
?>
<a href="test.php?term=swing">Swing</a>

I’m away from the PC, I’ll try a bit later

Thanks

The above code gives

array(0) { } Swing

if i click the link i get

array(1) { [“term”]=> string(5) “swing” } Swing

Right? So is that what you want to happen?

User clicks on an image of a swing (say) and searchresults.php gets the term “swing”, which then sends it to your search and displays the results.

As your existing search already works seemingly you will just need to change $_POST to $_GET.

Or, set your script to work with BOTH arrays… I’ve done that before.

I have two files now.

First one ‘test.php’

<a href=“result.php?term=swing”>Swing</a>

second one ‘result.php’

<?PHP
$term=$_GET['term'];
echo $term;
?>

This works fine

It still doesn’t work on the searchresults page.

I think it probably has to do with something with two commands relating to the same variable $term.

$term=$_GET['term'];
$term = $_POST['term'];

I think I need some type of

if $_post is empty then look for $_get to avoid confusion

I wish I had seen this before lol

I changed POST to GET and it’s working.

That’s great.

Many thanks

I spoke too soon, the POST on the search form isn’t playing now.

I think I need some type of

if $_post is empty then look for $_get to avoid confusion

That was what I ended up doing, much later I discovered $_REQUEST :wink:

“An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.”

http://php.net/manual/en/reserved.variables.request.php

That’s brilliant Cups

REQUEST is working for both forms and html links

You’re a star, much appreciated.

Another little php nugget of information added to my code