【问题标题】:Updating multiple input values using one submit button in a loop在循环中使用一个提交按钮更新多个输入值
【发布时间】:2015-11-16 10:55:32
【问题描述】:

在会话中存储了一个数组。我已经使用 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>

【问题讨论】:

  • 放在循环之前会给我一个错误 Undefined index: aaa and Undefined index: ke。并且它不会更新 antying :(

标签: php arrays forms loops


【解决方案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'] 键。

【讨论】:

  • 是的!感谢大家的帮助。但这是有效的代码。 Audite Marlowked 谢谢
  • 感谢大家的帮助。我已经更新了有效的代码:)
【解决方案2】:

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

如果你想一键更新所有元素,你必须在“foreach”块之前打开form标签,在foreach块外面关闭/form标签

【讨论】:

    【解决方案3】:

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

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

      【讨论】:

      • 感谢您的回复,但此代码有一个额外的输入框,当我更新时出现错误,提示此未定义索引:aaa 未定义索引:ke
      • 看名字,是用key更新的,所以不能用aaa或者ke调用,可以用$key调用
      • Vijaya Sankar N--- 这个代码也很完美!非常简单的解释非常感谢!
      • 很高兴我帮助了你!
      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 2011-05-28
      相关资源
      最近更新 更多