duanmoen784988 2013-02-07 11:13
浏览 67
已采纳

PHP多图像按钮提交表单

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?

  • 写回答

5条回答 默认 最新

  • duanlie1298 2013-02-07 11:19
    关注
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)