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

报告相同问题?

悬赏问题

  • ¥20 安装 opencv4nodejs 报错
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!