Possible Duplicate:
Can I get the name of submit button in another form?
Hi,
I have a form which has 3 submit buttons. Their names are generated and assigned in a loop. Now if I use a post method, how can access the name of the submit button which was clicked.
The following is the example of my form:
<form name="one" method="post" action="two.php">
<?php
while($i=1;$i<=3;$i=$+1)
{
?>
<button type="submit" name="<?php echo $i ?>" value="<?php echo $i ?>" >
</button>
<?php
}
?>
</form>
May be I can use onsubmit attribute for the button tag in one.php, but开发者_开发百科 I am unable to get the output. Any suggestions?
You can examine the $_POST
array to see which number was sent through.
How you do that is entirely up to you, it could be as basic as a few isset()
checks.
if (isset($_POST['1'])) {
echo "Clicked button 1";
}
if (isset($_POST['2'])) {
echo "Clicked button 2";
}
if (isset($_POST['3'])) {
echo "Clicked button 3";
}
It would probably make more sense to use the same name and different values for multiple submit buttons.
<form name="one" method="post" action="two.php">
<?php
while($i=1;$i<=3;$i=$+1)
{
echo "<input type='submit' name='submitButton' value='{$i}' />";
}
?>
</form>
In two.php
you can then simply pull the value of the submit button that was pressed using $_POST['submitButton']
精彩评论