按下哪个图像(表格)

  • 本文关键字:表格 图像 php html
  • 更新时间 :
  • 英文 :


,所以我只有由几个 <input type="image">组成的表格,但是我很难重述按压图像。示例:

<form method="post">
    <input type="image" src="y1.jpg" name="y1">
    <input type="image" src="y2.jpg" name="y2">
</form>
<?php
    if(isset($_POST['y1'])) {
        //Pressing the first image does not return this
        echo "You've pressed image y1!";
    }
    if(isset($_POST['y2'])) {
        //Pressing the second image does not return this
        echo "You've pressed image y2!";
    }
?>

我猜这个问题在每个图像中的某个位置,例如单个提交按钮的同一表格,但是每个图像对我来说听起来像是不良练习。

我知道这里可能错过了一些非常愚蠢的东西,对不起。任何帮助将非常感激。谢谢。

您可以说: -

<?php
    if(isset($_POST['y1'])) {
        //Pressing the first image does not return this
        echo "You've pressed image y1!";
    }
    if(isset($_POST['y2'])) {
        //Pressing the second image does not return this
        echo "You've pressed image y2!";
    }
?>
<form method="post">
    <input type="image" src="y1.jpg" value="1" name="y1">
    <input type="image" src="y2.jpg" value="1" name="y2">
</form>

您只需要输入内的值

您可以使用var_dump($ _ post)检查收到的内容。在没有值的图像输入的情况下,它将位置发送到您单击图像中的_x,_y后缀。

如果您只想识别单击的内容,请添加像Muthafury回答的值。

您的输入类型是图像,因此您可以轻松检查,因为输入名称为name_x and name_y

if(isset($_POST['y1_x'], $_POST['y1_y']))
if(isset($_POST['y2_x'], $_POST['y2_y']))

,由于您不关注 value属性,因此键存在于 $_POST array中,但是,内容实际上是 empty 。。

设置值或检查键是否存在。

if(key_exists('y1', $_POST))

最新更新