在循环中使用一个提交按钮更新多个输入值


Updating multiple input values using one submit button in a loop

我有一个数组存储在会话中。我已经使用foreach循环回显了每个键和值。每个键旁边都有一个输入框,用于更新该特定键的值。

问题是,每个输入框都有自己的提交按钮,用于更新值。我想让它成为唯一一个更新所有输入框的提交。

我尝试放置提交按钮和循环的外部。但这只会更新循环中的最后一个值,而不会更新任何其他值。我试着把它放在php之外,并把它重写为html,但由于某种原因,它仍然不起作用。

提前感谢!

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
}

?>

更新所以我使用了Vijaya Sankar N的代码和Audit Marlow的代码,它们都能完美地工作。

由Audit 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>

将表单放在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>

现在,在您的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;
    }
}

这样,您将为每次输入更新所有$_SESSION['animals']键。

问题不在于按钮的位置,而在于form和/form标记单击表单/表单块中的按钮时,浏览器会发送该块中的所有数据。

如果你想用一个按钮更新所有元素,你必须打开"foreach"块之前的form标记,并关闭foreach块外部的/form标记

只需用一个提交按钮将所有内容放在一个表单中。您需要使输入名称唯一,否则只会提交最后一个值。我通过创建一个"animal"输入数组来实现这一点。在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>

将表单和按钮移动到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>

您的PHP提交代码将简单到:

foreach($_POST as $key=>$value){
    $_SESSION['animals'][$key] = $value;
}