【问题标题】:PhP adding arrays to arrays from hidden fieldsPhP将数组添加到隐藏字段中的数组
【发布时间】:2015-07-02 15:50:48
【问题描述】:

我正在尝试通过 HTML 表单和隐藏字段向 php 中的数组添加值。首先,我从我的 php 文件中的帖子中获取值,然后像 index.php 一样将值添加到数组中:

<?php
    $gamelist = filter_input(INPUT_POST, 'gamelist',FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
    $key = filter_input(INPUT_POST, 'gameID', FILTER_VALIDATE_INT);
    $team1 = filter_input(INPUT_POST, 'teamID1', FILTER_VALIDATE_INT);
    $team2 = filter_input(INPUT_POST, 'teamID2', FILTER_VALIDATE_INT);
    $date = filter_input(INPUT_POST, 'date');

if ($gamelist === NULL) {
    $gamelist = array();


    }

    if ($new_game === NULL) {
    $new_game = array();


    }   


//get action variable from POST
$action = filter_input(INPUT_POST, 'action');

//initialize error messages array
$errors = array();

//process
switch( $action ) {
    case 'Add Game':



        $new_game[] = $date;
        $new_game[] = $team1;
        $new_game[] = $team2;


        $gamelist[] = $new_game;

        break;
    case 'Delete Game':
        $game_index = filter_input(INPUT_POST, 'gameID', FILTER_VALIDATE_INT);
        if ($game_index === NULL || $game_index === FALSE) {
            $errors[] = 'The game cannot be deleted.';
        } else {
            unset($gamelist[$game_index]);
            $gamelist = array_values($gamelist);
        }
        break;


}

include('gamelist.php');
?>

此代码的输出如下所示。这正是我想要的。

Array
(
    [0] => Array
        (
            [0] => 2/30/15
            [1] => 1
            [2] => 2
        )

)

但是,当另一个游戏被添加时(当另一个数组被添加到 $gamelist[] 时)该数组会忘记之前的数组并变成这样:

Array
(
    [0] => Array
    [1] => Array
        (
            [0] => 5/17/2015
            [1] => 2
            [2] => 3
        )

)

我需要 [0] 数组来保留我之前传递给它的值。

有什么想法???

下面是带有隐藏字段的完整 html 端 (gamelist.php):

    <?php

require('../database.php');
$query = 'SELECT * FROM team_table';
$statement = $db->prepare($query);
$statement->execute();
$teams = $statement->fetchAll();
$team = $statement->fetch();
$statement->closeCursor();
?>



<!DOCTYPE html>
<html>
<head>
    <title>RP LL Game List</title>



    <link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
<header>
<h1>RP LL Game List</h1>
</header>
<main>


<!-- Dumping gamelist and errors arrays -->





    <h2>Errors:</h2>
<?php if (count($errors) == 0) : ?>
<p>There are no errors</p>
    <?php else: ?>

    <ul>
        <?php foreach($errors as $error) : ?>
            <li><?php echo $error; ?></li>
        <?php endforeach; ?></ul>
<?php endif; ?>


<h2>Games:</h2>
<?php if (count($gamelist) == 0) : ?>  
    <p>There are no games in the game list.</p>
        <?php else: ?>
            <ul>
            <?php print_r($gamelist); ?>

                <?php foreach($gamelist as $game)  : ?> 

                        <li><?php print_r($game); ?></li> 
                <?php endforeach; ?>
            </ul>
<?php endif; ?>
<br>

<!-- Add Game Form -->

<h2>Add Game:</h2>
    <form action="." method="post" > 
        <?php foreach( $gamelist as $game ) : ?>
<input type="hidden" name="gamelist[]" value="<?php echo $game; ?>">
<?php print_r($gamelist); ?>
<?php endforeach; ?>






        <label>Team1:</label>
        <select  name="teamID1">
            <?php foreach ($teams as $team) : ?>
                <option value="<?php echo $team['team_id']; ?>">
                    <?php echo $team['name_col']; ?>
                </option>
            <?php endforeach; ?>

            </select><br>

        <label>Team2:</label>
        <select  name="teamID2">
            <?php foreach ($teams as $team) : ?>
                <option value="<?php echo $team['team_id']; ?>">
                    <?php echo $team['name_col']; ?>
                </option>
            <?php endforeach; ?>

            </select><br>

        <label>Date:</label>
        <input type="text" name="date">


<input type="submit" name="action" value="Add Game">
</form>
<br>


<?php if (count($gamelist) > 0 && empty($game_to_modify)) : ?>
<h2>Select game:</h2>
<form action="." method="post" >
<?php foreach( $gamelist as $key => $game ) : ?>
<input type="hidden" name="gamelist[]" 
 value="<?php print_r($game[$key]); ?>">
<?php endforeach; ?>



<label>Game:</label>
<select name="gameID">
<?php foreach( $gamelist as $key => $game ) : ?>
<option value="<?php echo $game[$key]; ?>">
    <?php echo $game; ?>
</option>
<?php endforeach; ?>
</select>



<input type="submit" name="action" value="Delete Game">




</form>
<?php endif; ?>



</main>
</body>
</html>

【问题讨论】:

  • 发布您的完整代码。你的数组不可能有这个代码的两个值。您必须在其他地方初始化/存储 $gamelist,这可能就是问题所在。
  • 已发布完整代码。

标签: php arrays hidden-field


【解决方案1】:

每次您提交表单时,值 vl 不会被填充,而是尝试使用 session 以便您可以在所需数组中包含以前的值

【讨论】:

    猜你喜欢
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 2019-11-02
    • 2013-05-20
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    相关资源
    最近更新 更多