I have been creating a website where my users can rate images that have been uploaded. Currently i use radio buttons and a submit button to get the users rating and save it within the system. I would like to change this so that the code uses image clicks to get the users rating. So far i have the following code however it only works for one image button. Is there any way to change this to get the result depending on what image has been clicked.
Code:
HTML
<form>
<input
type="image"
name="flag_submit"
src="img/Flag.png"
onmouseover="this.src='img/Flag_Click.png'"
onmouseout="this.src='img/Flag.png'"
height="30"
width="30"
/>
</form>
PHP
if (isset($_POST['flag_submit_x']))
{
//Do process of rating.
}
Is there any way that i could create multiple image buttons and in the PHP code detect what image button has been pressed?
Here's a trick that might be of help:
I have created an HTML page of the form:
CODE
The script to which the form submits, show_post.php, reads:
CODE
When I click on the first image, I get:
When I click on the second image, I get:
This works with Opera, IE, and Mozilla.
Change name to an array
name="flag_submit[1]"
. Assign a different value for each image and you got it.Read it as an array on php side:
if (isset($_POST['flag_submit'][1]))
.Or better would be, loop throu if
$_POST['flag_submit']
and find all values:In your case, you don't care, what value it sends, all you need to care about it is the key that was used to submit the form, because there will always be only one key set.
this might help you
The First code works fine for me, the Change name to an array name="flag_submit[1]". Assign a different value for each image and you got it.
Read it as an array on php side:
You can have multiple <button type="submit"> elements with the same name but different values that can contain the images, only the value of the one that has been clicked will be sent.
For more info, see the specification: http://www.w3.org/html/wg/drafts/html/master/forms.html#the-button-element