Get the name of submit button in PHP

孤者浪人 提交于 2019-11-29 22:35:35

问题


How do I get the name of the submit button in PHP?

I got the value of submit button, but I could not find any code to get the name of the button. Below is the code I have written.

<form name="form1"  method="post">
    <input type="submit" value="hello" name="submitbutton">
</form>

<?php
    echo "Value of submit button: " . $_POST['submitbutton'];
    echo "Name of submit button: " . // What should I do here? //;
?>

回答1:


You will find the name in the $_POST array.

<?php
print_r($_POST);
?>

This way you will see everything in the $_POST array.

You can iterate through the array with:

foreach($_POST as $name => $content) { // Most people refer to $key => $value
   echo "The HTML name: $name <br>";
   echo "The content of it: $content <br>";
}



回答2:


'submitbutton' is the name of your submit button. you can get the names of super global $_POST array elements with array_keys() function

$postnames = array_keys($_POST);



回答3:


Its like any other POST variable, the name will be in the $_POST array. The name is the key in the $_POST array.



来源:https://stackoverflow.com/questions/7074931/get-the-name-of-submit-button-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!