PHP multiple image button submit form

被刻印的时光 ゝ 提交于 2019-12-07 08:19:28
  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.

Here's a trick that might be of help:

I have created an HTML page of the form:

CODE

<html><body><form method="post" action="show_post.php">
<input type="image" name="stamp[1134118800]" src="redstar.gif" value="red">
<input type="image" name="stamp[1134140400]" src="greenstar.gif" value="green">
</form></body></html>

The script to which the form submits, show_post.php, reads:

CODE

<?php
print '<html><body><pre>';

print_r ($_POST);

print '</pre></body></html>';
?>

When I click on the first image, I get:

Array
(
    [stamp] => Array
        (
            [1134118800] => 21
        )

)

When I click on the second image, I get:

Array
(
    [stamp] => Array
        (
            [1134140400] => 15
        )

)

This works with Opera, IE, and Mozilla.

Prince Adeleye Dairo

The First code works fine for me, the Change name to an array name="flag_submit[1]". Assign a different value for each image and you got it.

Read it as an array on php side:

if (isset($_POST['flag_submit'][1]))

You can have multiple <button type="submit"> elements with the same name but different values that can contain the images, only the value of the one that has been clicked will be sent.

For more info, see the specification: http://www.w3.org/html/wg/drafts/html/master/forms.html#the-button-element

this might help you

<?php
if($_POST['button'])
{
    echo "you have pressed button ".$_POST['button'];
}
?>
<style>
    input.overridecss {
    background-color: transparent;
    border: 0px;
    background-position: center;
    background-repeat: no-repeat;
    background-image: url(http://i49.tinypic.com/rm2w0i.png);
}
</style>
<form method="POST">
    <input type="submit" name="button" value="1" class="overridecss"/>
    <input type="submit" name="button" value="2" class="overridecss"/>
    <input type="submit" name="button" value="3" class="overridecss"/>
    <input type="submit" name="button" value="4" class="overridecss"/>
    <input type="submit" name="button" value="5" class="overridecss"/>
</form>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!