PDA

Click to See Complete Forum and Search --> : Assign & retrieve value from image in PHP


systech44
Jan 23rd, 2010, 12:46 PM
Hello,

I have a serious situation.

I have 10 different values in an array of PHP.
I have 10 different images in an HTML form.
I have to assign these 10 array values to 10 images. If I click an image then the corresponding value will be display.

My PHP code is below.


<?php

if(isset($_POST["img"]))
{
echo $_POST["img"];
}

?>


But this is not working. Please help to overcome the situation.

Thanks & Regards.

kows
Jan 23rd, 2010, 01:03 PM
uh. how about you show us your form because I'm not entirely sure what you're doing.

Krokonoster
Jan 24th, 2010, 07:52 AM
Agree with kows.

Sound like you need to look at multi-dimensional arrays. (see w3schools (http://www.w3schools.com/php/php_arrays.asp)or php.net (http://php.net/manual/en/language.types.array.php), depending on your level)

If you want to investigate what's posted use:
print_r($_POST);

systech44
Jan 24th, 2010, 10:41 AM
Here is my code. Please help.


<?php

if(isset($_POST["img"]))
{
echo $_POST["img"];
}
else
{
$arr = array(1,3,6,87,23,7,33,12,5,54);
}

?>



<html>
<head>
<title>Image</title>
</head>
<form name="f" method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>">
<body>
<img id='$arr[0]' src='a.png' />
<img id='$arr[1]' src='a.png' />
<img id='$arr[2]' src='a.png' />
<img id='$arr[3]' src='a.png' />
<img id='$arr[4]' src='a.png' />
<img id='$arr[5]' src='a.png' />
<img id='$arr[6]' src='a.png' />
<img id='$arr[7]' src='a.png' />
<img id='$arr[8]' src='a.png' />
<img id='$arr[9]' src='a.png' />
</body>
</form>
</html>

kows
Jan 24th, 2010, 11:24 AM
uhh.. but none of these images can be clicked on -- you don't have a link (<a>nchor tag) associated with them or any javascript to submit the form when you click on them. how do you expect PHP to interact with these? there's no way.

I still don't know what you're trying to do. instead of explaining an abstract situation, try explaining exactly what you want to do, with as much detail as possible to get the point across.

as a side note, you're defining your IDs wrong. IDs can't be defined the way you're doing it (with scalars ['$'] and square brackets), and IDs are not used by PHP (whereas in ASP.NET, they are). PHP uses the 'name' attribute associated with an <input> when you're dealing with POST. if you want to use images, you'll have to do something fancy with GET. it all depends on what you're actually doing, though.