会话数组中的某些项目被点击后取消设置?

时间:2015-04-29 04:20:52

标签: php html arrays session unset

我基本上寻找的是能够在单击该项目时从阵列中删除某个项目,对于此实例...如果我点击了#34;两个",它将消失

演示:http://query.notesquare.me

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
    ini_set("session.save_path", "/home/kucerajacob/public_html/query.notesquare.me/test-sessions");
    session_start();

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $create_playlist = $_POST['create_playlist'];

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

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

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

    $_SESSION['main'] = $playlists;

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

2 个答案:

答案 0 :(得分:1)

可能,您也需要处理该请求。如果您希望发布click,则旁边的简单<button>就足够了。

在渲染标记时,(当然使用会话数组)使用可用于取消设置值的键。

<?php

// initialization
if(empty($_SESSION['user_playlists'])) {
    $_SESSION['user_playlists'] = array("One", "Two", "Three");
}

if(isset($_POST['add'], $_POST['create_playlist'])) {
    // handle additions
    $_SESSION['user_playlists'][] = $_POST['create_playlist'];
}

if(isset($_POST['remove'])) {
    // handle remove
    $key = $_POST['remove'];
    unset($_SESSION['user_playlists'][$key]);
}

?>

<form method="post">
    <input type="text" id="input-create-playlist" placeholder="Playlist Name" name="create_playlist" />
    <input type="submit" id="button-create-playlist" name="add" value="Create Playlist" />
    <hr/>
    <?php foreach($_SESSION['user_playlists'] as $k => $p): ?>
        <?php echo $p; ?>&nbsp;<button type="submit" name="remove" value="<?php echo $k; ?>">Remove</button><br/>
    <?php endforeach; ?>
</form>

Sample Demo

答案 1 :(得分:0)

尝试以下:

for ($i = 0; $i < count($playlists); $i++) {
       // echo $playlists[$i] . "<br />";
  printf('<a href="php_pagename.php?query=%1$s">%1$s</a><br/>', $playlists[$i]);
}

if ($_GET['query'])
{
unset($playlists['query']);
}