array_push在页面刷新后不添加第二项

时间:2015-04-12 08:34:32

标签: php array-push

我知道array_push没有添加第二项,因为在添加另一项后页面刷新时,原来的项目会消失,并被文本框中的最新条目替换。

我正试图实现这种尝试的策略......

a。让会话记住通过页面刷新即将添加的下一个项目。

b。根本不刷新页面。

在此处查看演示:http://query.notesquare.me/home.html/

CODE

<form method="post">
    <input type="text" id="input-create-playlist" placeholder="Playlist Name" name="create_playlist" />
    <input type="submit" id="button-create-playlist" value="Create Playlist" />
</form>

<?php
    session_start();

    $create_playlist = $_POST['create_playlist'];

    $playlists = array("One", "Two", "Three");

    $_SESSION['main'] = $playlists;

    array_push($playlists, $create_playlist);

    for ($i = 0; $i < count($playlists); $i++) {
        echo $playlists[$i] . "<br />";
    }
?>

1 个答案:

答案 0 :(得分:2)

尝试

<form method="post">
    <input type="text" id="input-create-playlist" placeholder="Playlist Name" name="create_playlist" />
    <input type="submit" id="button-create-playlist" value="Create Playlist" />
</form>

<?php
    session_start();

    $create_playlist = $_POST['create_playlist'];

    $_SESSION['user_playlists'][] = $create_playlist;

    $playlists = array("One", "Two", "Three");

    for ($i = 0; $i < count($_SESSION['user_playlists']); $i++) {
        array_push($playlists, $_SESSION['user_playlists'][$i]);
    }

    $_SESSION['main'] = $playlists;      

    for ($i = 0; $i < count($playlists); $i++) {
        echo $playlists[$i] . "<br />";
    }
?>

您的方法是在$_SESSION['main'] = $_POST['create_playlist']所需的效果之前设置array_push