...

View Full Version : making an image a link



ciaracous
08-23-2006, 02:42 PM
Hi,

I'm just wondering how I can make an image into a link. I have a page which displays all the details on all of the products I have stored in my database and I have an image of each product beside them. What I am doing at the moment is typing the name product id in and clicking on a button to update it which sends all the details on the product to a new page where they can be updated. What I want to do is just click on the image and have it send all the details to the next page, rather than having to type in a product id everytime so as to cut down on human error.

I have the table with the details displayed as follows:



<td align="center"><img src="http://snet.wit.ie/~ciaracousins/clothes/' . $row['image'] . '">
<td align="center">'.$row['prodId'].'</td>
<td align="center">'.$row['shopName'].'</td>
<td align="center">'.$row['prodName'].'</td>
<td align="center">'.$row['dept'].'</td>
<td align="center">'.$row['brand'].'</td>
<td align="center">'.$row['type'].'</td>
<td align="center">'.$row['image'].'</td>
<td align="center">'.$row['price'].'</td>

Then to send the information to the next page I was using this when the user clicked on the button to update


<form action="admin_delete.php?login=true&shopName=<?php echo $shopName; ?>" method="post">

So I want to be able to do the same thing but by clicking on the image rather than entering an id number and clicking on the update button

Crowds
08-23-2006, 04:06 PM
How about something like..

<input type="image" name="<? echo $row['prodId'] ?>" alt="Send" value="Send" src="<? $row['image']?>">

?

Crowds

Fumigator
08-23-2006, 04:12 PM
I would just use Javascript and make your image a link with an onclick event that sets the value of a hidden input field and then submits your form. You'll have to give your form a name, add a hidden input field, and imbed the product ID.



<a href="#" onclick="submitIt(\'$productID\');"><img src="http://snet.wit.ie/~ciaracousins/clothes/' . $row['image'] . '"></a>

.
.
.

<script type="text/Javascript">
function submitIt(id) {
document.form1.hiddenID.value = id;
document.form1.submit();
}
</script>

ciaracous
08-23-2006, 04:25 PM
thanks, ok I got it working using



<td align="center"><a href="admin_update.php?login=true&shopName='.$row['shopName'].'&prodId='.$row['prodId'].'"><img src="http://snet.wit.ie/~ciaracousins/clothes/' . $row['image'] . '"></a>



But what is happening now is that when I get to my admin_update.php the table isnt showing up. What is being passed through is the exact same as what was passed when i used the update button, but its not working when I do it this way. Should the same code not work for this since its the same values being passed in??

Here is the code I am using for my admin_update page:



<?php

include "db.php";

$prodId = $_GET['prodId'];
$shopName = $_GET['shopName'];
$query="select shopId from shops where shopName=$shopName";


$sql= "select * from product where prodId=$prodId";




if(isset($_POST['submittedUpdate'])){




$prodId= trim($_POST['prodId']);
$sName = trim($_POST['shopName']);
$prodName= trim($_POST['prodName']);
$dept = trim($_POST['dept']);
$brand = trim($_POST['brand']);
$type = trim($_POST['type']);
$image = trim($_POST['image']);
$price = trim($_POST['price']);






$query2 = "UPDATE product SET prodName='$prodName', dept='$dept', brand='$brand', type='$type', image='$image', price='$price' WHERE prodId ='$prodId' and shopName='$shopName'";



$result = @mysql_query($query2);
if($result){
echo'<p align=center><font color="#333333"><b>UPDATED SUCCESSFULLY</b></font></p>';
}

else{
echo'<h1> System Error </h1>';
}



$query = "SELECT * FROM product WHERE prodId = '$prodId' and shopName='$shopName'";


$result = @mysql_query($query);
if($result){
echo'


<form action="admin_update.php" method="post">



<center>
<table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8">';

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo'


<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Product Id </b></center></td>
<td align="left">'.$row['prodId'].'</td>




<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Shop Name</b></td>
<td align="left">'.$row['shopName'].'</td>

.......

<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Price</b></td>
<td align="left"><input type="text" name="price" size="50" maxlength="50" value="'.$row['price'].'"/></td>



</tr>';
}
echo'


<tr><td><center><input type="submit" name="submit" value="UPDATE"/>
<input type="reset" value="CLEAR FORM"></p></center>


<input type="hidden" name="prodId" value='.$prodId.'>
<input type="hidden" name="shopName" value='.$sName.'>



<input type="hidden" name="submittedUpdate" value="TRUE"/></form></td></tr>

</table>

';
}

}

if(isset($_POST['submitted'])){

$prodId = trim($_POST['prodId']);
$shopName= $_GET['shopName'];





$query = "SELECT * FROM product WHERE prodId = '$prodId' and shopName='$shopName'";




$result = @mysql_query($query);
if($result){
echo'


<form action="admin_update.php" method="post">



<center>
<table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8">';

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo'


<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Product Id</b></center></td>
<td align="left">'.$row['prodId'].'</td>



<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Shop Name</b></td>
<td align="left">'.$row['shopName'].'</td>

.......

<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Price</b></td>
<td align="left"><input type="text" name="price" size="50" maxlength="50" value="'.$row['price'].'"/></td>





</tr>';
}
echo'


<tr><td><center><input type="submit" name="submit" value="UPDATE"/>
<!--<input type="reset" value="CLEAR FORM"></p></center>-->
<tr><td><center>



<input type="hidden" name="prodId" value='.$prodId.'>
<input type="hidden" name="shopName" value='.$shopName.'>



<input type="hidden" name="submittedUpdate" value="TRUE"/></form></td></tr>

</table>
';

}

else{
echo'<h1> System Error </h1> table ';
exit();
}

}
mysql_close();

?>



</body>
</html>

Fumigator
08-23-2006, 04:45 PM
if(isset($_POST['submittedUpdate'])){


Therein lies your problem. The $_POST variable does not get set if you arrive at the page from a regular normal standard link. Your $_GET variables are there, because you've loaded those into your url, but you have to submit the form to load up $_POST. That's why I suggested the method I did; your form gets submitted and your link emulates your submit button.

However, that value $_POST['submittedUpdate'] is probably your submit button, so you'll have to reproduce that variable using a hidden input field. Either that or change your update code.

ciaracous
08-23-2006, 05:18 PM
Thanks Fumigator,

I hadn't seen your 1st post. I am trying to use that method now. Is the code that you wrote underneath all I need


<script type="text/Javascript">
function submitIt(id) {
document.form1.hiddenID.value = id;
document.form1.submit();
}
</script>


Or do I still need to write more. I'm not really familiar with javascript (or php for that matter) so you'll have to forgive me. I just put the script code underneath where I had the table created, inside the php, and I changed the # to admin_update.php. Is that right??



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum