PHP多图像按钮提交表单 [英] PHP multiple image button submit form

查看:149
本文介绍了PHP多图像按钮提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在创建一个网站,让我的用户可以评估上传的图片。目前我使用单选按钮和提交按钮来获取用户评分并将其保存在系统中。我想改变这一点,以便代码使用图片点击来获得用户评分。到目前为止,我有以下代码,但它只适用于一个图像按钮。有什么方法可以根据点击的图像来改变这个结果吗?

代码:



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
/> gt ;
< / form>

PHP

  if(isset($ _ POST ['flag_submit_x']))
{
//执行评级过程。
}

是否有任何方法可以创建多个图像按钮并在PHP代码中检测已按下图像按钮? 将名称更改为数组名称= flag_submit [1]。为每个图像分配一个不同的值,然后获得它。 阅读php中的数组: if(isset($ _POST ['flag_submit'] [1]))


,循环如果 $ _ POST ['flag_submit'] 并找到所有值:

  foreach($ _POST ['flag_submit'] as $ value){
echo $ value。 '已被点击。';






 < form method =post> 
< input type =imagename =rateButton [1]src =img / Rate1.pngheight =40width =40value =1/> T
< input type =imagename =rateButton [2]src =img / Rate1.pngheight =40width =40value =1/> T
< input type =imagename =rateButton [3]src =img / Rate1.pngheight =40width =40value =1/> T
< input type =imagename =rateButton [4]src =img / Rate1.pngheight =40width =40value =1/> T
< / form>
< pre>
<?php
if(isset($ _POST ['rateButton'])){
foreach($ _POST ['rateButton'] as $ key => $ value){
echo'图像号码'。$ key''被点击。';
}
}
?>

在您的情况中,您不在意,它发送了什么值,您只需要关心它是用来提交表单的关键,因为总是只有一个键集。


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?

解决方案

  1. Change name to an array name="flag_submit[1]". Assign a different value for each image and you got it.

  2. 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:

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.

这篇关于PHP多图像按钮提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆