drqvr26084
2014-08-03 22:50
采纳率: 100%
浏览 49
已采纳

PHP - 使用$ _POST更新会话数组

I'm new to using PHP sessions and I'm having trouble updating a session array using the form $_POST method.

Basically, each button has a value that I want to add to the array $_SESSION['items'] on each click. However, currently what is happening is the array gets reset and replaced by the last selected value.

Here's what I've got so far:

<?
session_name("test");
session_start();
?>


<form method="post">
    <button type="submit" name="item[]" value="Item 1">Item 1</button>
    <button type="submit" name="item[]" value="Item 2">Item 2</button>
</form>


<?
$_SESSION['items'] = array();
array_push($_SESSION['items'], $_POST['item']);
print_r($_SESSION['items']);
?>


Any help would be appreciated.
Thanks!

4条回答 默认 最新

相关推荐 更多相似问题