array_push 页面刷新后未添加第二项

array_push not adding 2nd item after page refresh

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

我正在努力实现这种尝试要么...

a. 让会话记住要添加的下一个项目,即使是通过页面刷新也是如此。

b.就是根本不刷新页面。

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

代码

<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 />";
    }
?>

尝试

<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 />";
    }
?>

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