如何在php中制作一个带有变量的可点击图像到另一个页面?

My question is pretty simple. I tried doing some research into this but I never got it working with images.

I want to have a clickable image so when a user clicks on that image, it carries over some type of variable (ID) to another php page, so I can know which image that user clicked on and give the result.

My current code is this:

    echo '<a href="viewer.php"><img  src=' . $covers->src . 'height="300" width="190" value=' . $test . ' name="view" /></a>';

I tried doing this on my second php page:

$var_value = $_GET['view];
echo $var_value;

but I get undefined error on line 1.

You try to fetch a parameter called view with $_GET. Simply add a parameter to your link called view and then its value.

echo '<a href="viewer.php?view=theValueIWantToBringAlong"><img  src="' . $covers->src . '" height="300" width="190" name="view" /></a>';

In this example, using

echo $_GET['view'];

when at viewer.php will print "theValueIWantToBringAlong".

If your value is is stored in a variable, then do like this:

$myValue = 'theValueIWantToBringAlong';
echo '<a href="viewer.php?view='.$myValue.'"><img  src="' . $covers->src . '" height="300" width="190" name="view" /></a>';

You want to put the values in the href of the <a> tag, not within the attributes of the <img>.

Change:

echo '<a href="viewer.php"><img  src=' . $covers->src . 'height="300" width="190" value=' . $test . ' name="view" /></a>';

to:

echo '<a href="viewer.php?view=' . $test . '"><img src=' . $covers->src . 'height="300" width="190"/></a>';

An <a> isn't like a <form>. It can't pass values from elements inside of it to the link. The only way to do this with an <a> is to put the values within the href. The key/value pairs after the ? are then accessible in $_GET.

value and name are not valid attributes for img, see w3schools.com.

Why not simply:

echo '<a href="viewer.php?view="' . $test . '">';
echo '<img  src="' . $covers->src . '" height="300" width="190"/></a>';

Note also that you did not provide quotes around the src value of the image and that you had no space before height.

echo "<a href=viewer.php?view={$test}><img src='{$covers->src}' height=300 width=190 name=view /></a>";