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条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?