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? //;
?>
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>";
}
'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);
Its like any other POST variable, the name will be in the $_POST array. The name is the key in the $_POST array.
Related
I have a problem with <form> in php .
i have uncertain number of fields (inputs) . it may be 1 or 100 input field .
Is there any function or class to get uncertain number of fields ?
Try that :
echo '<form>';
foreach ($data as $value) {
echo '<input name="field[]" value="',$value,'">';
}
echo '<input type="submit"></form>';
If you send that form, $_POST['field'] will be an indexed array in which every entry will correspond to one of the inputs.
$_POST would contain all the fields from the form on submit
print_r($_POST) will display them in an array for you
You can use an array name for the input fields in your form. For example, you can make an array of title fields by adding [] after the name:
<input type="text" name="title[]" />
<input type="text" name="title[]" />
<input type="text" name="title[]" />
Now in your PHP code, this value will be an array containing an amount of values equal to the number of fields with this name. The following code would print all titles on separate lines:
foreach ($_REQUEST['title'] as $value)
echo $value . "\n";
Just count the $_POST or $_GET what you use.
<?php
if(isset($_POST['submit'])){
echo "The total number of input fields is";
echo count($_POST); // include submit also
}
?>
I have an array stored inside a session. I have echoed out each key and value using a foreach loop. Next to each key there is an input box for updating the value for that specific key.
The problem is that, each input box has its own submit button for updating the value. I want to make it only one submit that updates all input boxes.
I tried placing the submit button and the outside of the loop. But that only updates the last value in the loop and not any other one.
I tried having it even outside the php and rewriting it as html, but it still didnt work for some reason.
THANKS in advance!
MY CODE!
<?php
// begin the session
session_start();
// create an array
$my_array=array('cat', 'dog', 'mouse');
// put the array in a session variable
if(!isset($_SESSION['animals']))
$_SESSION['animals']=$my_array;
// move submit code outside of foreach loop
if (isset($_POST["submit"]))
{
$aaa = $_POST['aaa'];
$key_var = $_POST['ke'];
// setting the session spesific session array value different for each key
$_SESSION['animals'][$key_var] = $aaa;
}
// loop through the session array with foreach
foreach($_SESSION['animals'] as $key=>$value)
{
// and print out the values
echo 'The value of key ' .$key. ' is '."'".$value."'".' <br />';
echo "update the value of key " .$key. " in the input box bellow";
// getting the updated value from input box
?>
<form method="post" action="">
<input type="text" name="aaa" value="<?php echo $value ; ?>" size="2" />
<!-- take a hidden input with value of key -->
<input type="hidden" name="ke" value="<?php echo $key; ?>">
<input type="submit" value="Update value of key" name="submit"/></div>
</form>
<?php
}
?>
UPDATE
So I used Vijaya Sankar N's code and Audite Marlow' code and they both work perfectly.
Updated code by Audite Marlow
<?php
// begin the session
session_start();
// create an array
$my_array=array('cat', 'dog', 'mouse');
// put the array in a session variable
if(!isset($_SESSION['animals']))
$_SESSION['animals']=$my_array;
// move submit code outside of foreach loop
if (isset($_POST["submit"]))
{
for ($i = 0; $i < count($_POST['aaa']); $i++) {
$aaa = $_POST['aaa'][$i];
$key_var = $_POST['ke'][$i];
// setting the session spesific session array value different for each key
$_SESSION['animals'][$key_var] = $aaa;
}
}
?>
<form method="post" action="">
<?php
// loop through the session array with foreach
foreach($_SESSION['animals'] as $key=>$value)
{
// and print out the values
echo 'The value of key ' .$key. ' is '."'".$value."'".' <br />';
echo "update the value of key " .$key. " in the input box bellow";
// getting the updated value from input box
?>
<input type="text" name="aaa[]" value="<?php echo $value ; ?>" size="2" />
<!-- take a hidden input with value of key -->
<input type="hidden" name="ke[]" value="<?php echo $key; ?>">
<?php
}
?>
<input type="submit" value="Update value of key" name="submit"/>
</form>
Put the form around your foreach loop. Put the submit button outside of your foreach loop, inside your form. Inside the foreach loop, make the names of your inputs an array, like so:
<form method="post" action="">
<?php
// loop through the session array with foreach
foreach($_SESSION['animals'] as $key=>$value)
{
// and print out the values
echo 'The value of key ' .$key. ' is '."'".$value."'".' <br />';
echo "update the value of key " .$key. " in the input box bellow";
// getting the updated value from input box
?>
<input type="text" name="aaa[]" value="<?php echo $value ; ?>" size="2" />
<!-- take a hidden input with value of key -->
<input type="hidden" name="ke[]" value="<?php echo $key; ?>">
<?php
}
?>
<input type="submit" value="Update value of key" name="submit"/></div>
</form>
Now, in your isset($_POST['submit']) { ... }, you want to loop through your input arrays like so:
if (isset($_POST["submit"]))
{
for ($i = 0; $i < count($_POST['aaa']); $i++) {
$aaa = $_POST['aaa'][$i];
$key_var = $_POST['ke'][$i];
// setting the session spesific session array value different for each key
$_SESSION['animals'][$key_var] = $aaa;
}
}
This way, you'll update all $_SESSION['animals'] keys for every input.
The problem isn't the position of the buttons, but the form and /form tags
When you click a button that is insiede a form /form block, the browser send all the data INSIDE the block.
If you want to update ALL the elements with one button, you have to open the form tag before the "foreach" block and close the /form tag outside the foreach block
Just put everything in one form with one submit button. Than you need to make the input name unique, cause else only the last value will be submitted. I did this by creating an 'animal' input array. In your PHP you can just simply loop through the POST data.
Try this:
<?php
// begin the session
session_start();
// create an array
$my_array=array('cat', 'dog', 'mouse');
// put the array in a session variable
if(!isset($_SESSION['animals']))
$_SESSION['animals']=$my_array;
// move submit code outside of foreach loop
if (isset($_POST["submit"]))
{
//Your new PHP to update all values
if(isset($_POST['animal']) && count($_POST['animal']) > 0)
{
foreach($_POST['animal'] as $key => $value)
{
$_SESSION['animals'][$key] = $value;
}
}
}
?>
<form method="post" action="">
<?php
// loop through the session array with foreach
foreach($_SESSION['animals'] as $key=>$value)
{
// and print out the values
echo 'The value of key ' .$key. ' is '."'".$value."'".' <br />';
echo "update the value of key " .$key. " in the input box bellow";
// getting the updated value from input box
?>
<input type="text" name="animal[<?= $key ?>]" value="<?= $value ?>" size="2" />
<?php
}
?>
<input type="submit" value="Update all values" name="submit"/></div>
</form>
Move the form and button outside the foreach and generate text fields with the key as identifier.
<form method="post" action="">
<?php
foreach($_SESSION['animals'] as $key=>$value)
{
echo 'The value of key ' .$key. ' is '."'".$value."'";
echo "update the value of key " .$key. " in the input box bellow <br />";
echo "<input type='text' name='$key' value='$value' size='2' /> <br />";
}
?>
<input type="submit" value="Update value of key" name="submit"/></div>
</form>
and your PHP submit code will be as simple as:
foreach($_POST as $key=>$value){
$_SESSION['animals'][$key] = $value;
}
I have a form which builds the form items from a foreach loop and puts a checkbox by each item:
<form action="nextStep.php">
<?php
foreach ($children[0] as $myPage) {
$menuname = $info[$myPage]['label'];
echo '<input type="checkbox" id="'.$menuname.'" name="reveal_menu" value="no" unchecked><label for="'.$menuname.'">'.$menuname.'</label><br>';
}
?>
<br>
<input type="submit" value="Submit">
</form>
As you can see, I start with each item unchecked. What I would like to know is how I should build the nextStep.php script to create individual php variables that I can echo on the nextStep.php page after the user clicks the submit button?
You must identify that this input is an array of values, by appending [] on the name:
echo '<input ... name="reveal_menu[]" ...>';
In nextStep.php:
foreach($_POST['reveal_menu'] as $checkbox)
echo $checkbox;
EDIT to answer OP comment:
You would need to create an array to handle these values. But $_POST['reveal_menu'] itself is an array. So can access $_POST['reveal_menu'][0], for example.
Keep in mind that $_POST['reveal_menu'] is an array with checked values ONLY . The index 0 doesn't point for the first checkbox of your form, but for the first checkbox checked from your form.
Each item needs to have a unique value, probably the menuname, so you can tell which ones are checked, and the name needs to have [] operator appended to the end.
<form action="nextStep.php">
<?php
foreach ($children[0] as $myPage) {
$menuname = $info[$myPage]['label'];
echo '<input type="checkbox" id="'.$menuname.'" name="reveal_menu[]" value="$menuname" unchecked><label for="'.$menuname.'">'.$menuname.'</label><br>';
}
?>
<br>
<input type="submit" value="Submit">
</form>
When you loop through, you'll get an array of values.
<?php
foreach($_POST['menuname'] as $v)
{
echo($v . 'Was checked.');
}
?>
I have form in my index.php that gets a value of name. In my action.php, I need to display the value using $_POST['name']. The name displays well when I directed into action.php However, I have another form in action.php that when I submit that form and refreshed action.php, the displayed name says an undefined index error for displaying name.
include('connect.php');
$name = $_POST['name'];
echo $name;
echo '<form method="get">';
//...
echo '<p><input type="submit" name="submit" value="GO" />';
echo '</form>';'
You are using the GET method while you should be using the POST method instead.
Change the following line:
echo '<form method="get">';
To:
echo '<form method="post">';
Check that $_POST['name'] is set using isset():
if(isset($_POST['name'])) {
$name = $_POST['name'];
echo $name;
}
For you to call $_POST['name'] from a form on a previous page, you will need to use form method = "GET" action = "targetpage.php"
use $_REQUEST["name"] it works for POST and GET Requests.
b.t.w. You should not echo Input data directly!! This can easily be used to do XSS Attacks. Use http://fr2.php.net/htmlentities or something like that before echoing a input directly to the browser! Otherwise a attacker could use something like alert('hax0rt'); as name ;)
Try this,
include('connect.php');
$name = isset($_POST['name']) ? $_POST['name'] : '';
echo $name;
echo '<form method="post">';
//...
echo '<input type="text" name="name"><br><p><input type="submit" name="submit" value="GO" />';
echo '</form>';'
I want if value field txtname was empty echo It is ok but it don't work in my code(if field was empty and click on button you see output with print_r(...)), Please see my demo and my code. what do I do?
Demo: http://codepad.viper-7.com/FNWcIs
<form method="post">
<input name="txtname[]">
<button>Click Me</button>
</form>
<?php
if ($_POST) {
$txtname = $_POST['txtname'];
if (!empty($txtname)) {
echo '<pre>';
print_r($txtname); // Output this is: Array ( [0] => )
} else {
echo 'it is ok';
}
}
?>
$txtname or ($_POST['txtname']) is an array with one element. Even that element is empty, empty() returns TRUE for any array that has one or more elements.
That should explain the behavior of your code.
To achieve what you're looking for, change the HTML:
From:
<input name="txtname[]">
To:
<input name="txtname">
If you don't use the brackets, it will be a string. And empty will return FALSE if it is empty. See Variables From External Sources PHP Manual.
change input field name to txtname like show below.
<input name="txtname">
Or if you want to use array of textboxes try below code.
<form method="post">
<input name="txtname[]">
<input name="txtname[]">
<input name="txtname[]">
<button>Click Me</button>
</form>
<?php
if($_POST){
$txtname = $_POST['txtname'];
foreach($txtname as $key=>$value)
{
if(!empty($value)){
echo '<br>'.$value; // Output this is: Array ( [0] => )
}else{
echo '<br>it is ok';
}
}
}
?>
You can try it this way it works if you are not intending to pass one value to the variable $txtname
`
$txtname = $_POST['txtname'];
if($txtname[0]){
echo '<pre>';
print_r($txtname); // Output this is: Array ( [0] => )
}else{
echo 'it is ok';
}
}
?>`