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.
我有一个存储在会话中的数组。我使用foreach循环回显了每个键和值。每个键旁边都有一个输入框,用于更新该特定键的值。
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.
我尝试放置提交按钮和循环外部。但这只会更新循环中的最后一个值,而不会更新任何其他值。我尝试在PHP之外使用它并将其重写为html,但它仍然因某些原因而无法正常工作。
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.
更新所以我使用了Vijaya Sankar N的代码和Audite Marlow的代码,它们都运行得很好。
Updated code by Audite Marlow
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>
4 个解决方案
#1
1
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:
将表单放在foreach循环周围。将提交按钮放在foreach循环之外,在表单内。在foreach循环内部,使输入的名称成为一个数组,如下所示:
<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:
现在,在你的isset($ _ POST ['submit']){...}中,你想循环输入数组,如下所示:
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.
这样,您将为每个输入更新所有$ _SESSION ['animals']键。
#2
0
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
如果要使用一个按钮更新所有元素,则必须在“foreach”块之前打开表单标记并关闭foreach块之外的/ form标记
#3
0
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中,您只需循环POST数据即可。尝试这个:
<?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>
#4
0
Move the form and button outside the foreach and generate text fields with the key as identifier.
将表单和按钮移动到foreach外部,并生成带有键作为标识符的文本字段。
<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:
并且您的PHP提交代码将如下所示:
foreach($_POST as $key=>$value){
$_SESSION['animals'][$key] = $value;
}
#1
1
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:
将表单放在foreach循环周围。将提交按钮放在foreach循环之外,在表单内。在foreach循环内部,使输入的名称成为一个数组,如下所示:
<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:
现在,在你的isset($ _ POST ['submit']){...}中,你想循环输入数组,如下所示:
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.
这样,您将为每个输入更新所有$ _SESSION ['animals']键。
#2
0
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
如果要使用一个按钮更新所有元素,则必须在“foreach”块之前打开表单标记并关闭foreach块之外的/ form标记
#3
0
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中,您只需循环POST数据即可。尝试这个:
<?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>
#4
0
Move the form and button outside the foreach and generate text fields with the key as identifier.
将表单和按钮移动到foreach外部,并生成带有键作为标识符的文本字段。
<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:
并且您的PHP提交代码将如下所示:
foreach($_POST as $key=>$value){
$_SESSION['animals'][$key] = $value;
}