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

时间:2015-11-16 10:55:33

标签: php arrays forms loops

存储在会话中的数组。我使用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的代码和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 个答案:

答案 0 :(得分:1)

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

现在,在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']个键。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

问题不在于按钮的位置,而在于形式和/表格标签 当您单击一个隐藏表单/表单块的按钮时,浏览器会在块中发送所有数据。

如果要使用一个按钮更新所有元素,则必须在“foreach”块之前打开表单标记并关闭foreach块之外的/ form标记

答案 3 :(得分:0)

只需使用一个提交按钮将所有内容放在一个表单中。比你需要使输入名称唯一,因为否则只提交最后一个值。我是通过创造一种动物来做到这一点的。输入数组。在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>