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?
有什么方法可以创建多个图像按钮,并在PHP代码中检测已按下什么图像按钮?
5 个解决方案
#1
3
-
Change name to an array
name="flag_submit[1]"
. Assign a different value for each image and you got it.将名称更改为数组名称=“flag_submit [1]”。为每个图像分配不同的值,你就得到了它。
-
Read it as an array on php side:
if (isset($_POST['flag_submit'][1]))
.在php端将其作为数组读取:if(isset($ _ POST ['flag_submit'] [1]))。
Or better would be, loop throu if $_POST['flag_submit']
and find all values:
或者更好的是,如果$ _POST ['flag_submit']循环,并找到所有值:
foreach ( $_POST['flag_submit'] as $value ) {
echo $value . ' has been clicked.';
}
<form method="post">
<input type="image" name="rateButton[1]" src="img/Rate1.png" height="40" width="40" value="1"/> T
<input type="image" name="rateButton[2]" src="img/Rate1.png" height="40" width="40" value="1"/> T
<input type="image" name="rateButton[3]" src="img/Rate1.png" height="40" width="40" value="1"/> T
<input type="image" name="rateButton[4]" src="img/Rate1.png" height="40" width="40" value="1"/> T
</form>
<pre>
<?php
if ( isset( $_POST['rateButton'] ) ) {
foreach ( $_POST['rateButton'] as $key => $value ) {
echo 'Image number '.$key.' was clicked.';
}
}
?>
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.
在您的情况下,您不关心,它发送什么价值,您需要关心的是用于提交表单的密钥,因为始终只有一个密钥集。
#2
1
Here's a trick that might be of help:
这是一个可能有帮助的技巧:
I have created an HTML page of the form:
我创建了一个表单的HTML页面:
CODE
<html><body><form method="post" action="show_post.php">
<input type="image" name="stamp[1134118800]" src="redstar.gif" value="red">
<input type="image" name="stamp[1134140400]" src="greenstar.gif" value="green">
</form></body></html>
The script to which the form submits, show_post.php, reads:
表单提交的脚本show_post.php读取:
CODE
<?php
print '<html><body><pre>';
print_r ($_POST);
print '</pre></body></html>';
?>
When I click on the first image, I get:
当我点击第一张图片时,我得到:
Array
(
[stamp] => Array
(
[1134118800] => 21
)
)
When I click on the second image, I get:
当我点击第二张图片时,我得到:
Array
(
[stamp] => Array
(
[1134140400] => 15
)
)
This works with Opera, IE, and Mozilla.
这适用于Opera,IE和Mozilla。
#3
1
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.
第一个代码对我来说很好,将名称更改为数组名称=“flag_submit [1]”。为每个图像分配不同的值,你就得到了它。
Read it as an array on php side:
在php端将其作为数组读取:
if (isset($_POST['flag_submit'][1]))
#4
0
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
有关详细信息,请参阅规范:http://www.w3.org/html/wg/drafts/html/master/forms.html#the-button-element
#5
0
this might help you
这可能对你有帮助
<?php
if($_POST['button'])
{
echo "you have pressed button ".$_POST['button'];
}
?>
<style>
input.overridecss {
background-color: transparent;
border: 0px;
background-position: center;
background-repeat: no-repeat;
background-image: url(http://i49.tinypic.com/rm2w0i.png);
}
</style>
<form method="POST">
<input type="submit" name="button" value="1" class="overridecss"/>
<input type="submit" name="button" value="2" class="overridecss"/>
<input type="submit" name="button" value="3" class="overridecss"/>
<input type="submit" name="button" value="4" class="overridecss"/>
<input type="submit" name="button" value="5" class="overridecss"/>
</form>
#1
3
-
Change name to an array
name="flag_submit[1]"
. Assign a different value for each image and you got it.将名称更改为数组名称=“flag_submit [1]”。为每个图像分配不同的值,你就得到了它。
-
Read it as an array on php side:
if (isset($_POST['flag_submit'][1]))
.在php端将其作为数组读取:if(isset($ _ POST ['flag_submit'] [1]))。
Or better would be, loop throu if $_POST['flag_submit']
and find all values:
或者更好的是,如果$ _POST ['flag_submit']循环,并找到所有值:
foreach ( $_POST['flag_submit'] as $value ) {
echo $value . ' has been clicked.';
}
<form method="post">
<input type="image" name="rateButton[1]" src="img/Rate1.png" height="40" width="40" value="1"/> T
<input type="image" name="rateButton[2]" src="img/Rate1.png" height="40" width="40" value="1"/> T
<input type="image" name="rateButton[3]" src="img/Rate1.png" height="40" width="40" value="1"/> T
<input type="image" name="rateButton[4]" src="img/Rate1.png" height="40" width="40" value="1"/> T
</form>
<pre>
<?php
if ( isset( $_POST['rateButton'] ) ) {
foreach ( $_POST['rateButton'] as $key => $value ) {
echo 'Image number '.$key.' was clicked.';
}
}
?>
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.
在您的情况下,您不关心,它发送什么价值,您需要关心的是用于提交表单的密钥,因为始终只有一个密钥集。
#2
1
Here's a trick that might be of help:
这是一个可能有帮助的技巧:
I have created an HTML page of the form:
我创建了一个表单的HTML页面:
CODE
<html><body><form method="post" action="show_post.php">
<input type="image" name="stamp[1134118800]" src="redstar.gif" value="red">
<input type="image" name="stamp[1134140400]" src="greenstar.gif" value="green">
</form></body></html>
The script to which the form submits, show_post.php, reads:
表单提交的脚本show_post.php读取:
CODE
<?php
print '<html><body><pre>';
print_r ($_POST);
print '</pre></body></html>';
?>
When I click on the first image, I get:
当我点击第一张图片时,我得到:
Array
(
[stamp] => Array
(
[1134118800] => 21
)
)
When I click on the second image, I get:
当我点击第二张图片时,我得到:
Array
(
[stamp] => Array
(
[1134140400] => 15
)
)
This works with Opera, IE, and Mozilla.
这适用于Opera,IE和Mozilla。
#3
1
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.
第一个代码对我来说很好,将名称更改为数组名称=“flag_submit [1]”。为每个图像分配不同的值,你就得到了它。
Read it as an array on php side:
在php端将其作为数组读取:
if (isset($_POST['flag_submit'][1]))
#4
0
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
有关详细信息,请参阅规范:http://www.w3.org/html/wg/drafts/html/master/forms.html#the-button-element
#5
0
this might help you
这可能对你有帮助
<?php
if($_POST['button'])
{
echo "you have pressed button ".$_POST['button'];
}
?>
<style>
input.overridecss {
background-color: transparent;
border: 0px;
background-position: center;
background-repeat: no-repeat;
background-image: url(http://i49.tinypic.com/rm2w0i.png);
}
</style>
<form method="POST">
<input type="submit" name="button" value="1" class="overridecss"/>
<input type="submit" name="button" value="2" class="overridecss"/>
<input type="submit" name="button" value="3" class="overridecss"/>
<input type="submit" name="button" value="4" class="overridecss"/>
<input type="submit" name="button" value="5" class="overridecss"/>
</form>