我在页面上有一个按钮,它是一个简单的输入提交按钮,它将页面ID发送到另一页面上的表单,以将其保存在PHP会话中。
我想知道如果该项目已经在您的会话中,我将如何显示删除按钮?
这是表单发送到
的代码<?php
session_start();
if(!in_array($_POST['event_id'], $_SESSION['event_orders'])) {
$_SESSION['event_orders'][] = $_POST['event_id'];
}
?>
带有按钮的页面实际上没有任何会话代码,但我想它会是类似的IF类型的语句,但我不确定我应该使用的确切语法?如何将ID从会话中移除?
编辑:
这样的事情会起作用吗?放置表单按钮。
<? if(isset($_SESSION['event_orders']))
{
echo "<a href='".$_SERVER['PHP_SELF']."?delete'>delete</a>";
}
else if(!in_array($_POST['event_id'], $_SESSION['event_orders'])) {
// echo the submit button;
} ?>
答案 0 :(得分:0)
您可以使用未设置的功能。参考:http://php.net/manual/en/function.unset.php
答案 1 :(得分:0)
<?php
session_start();
//if request parameter delete is present then unset the array
if(isset($_REQUEST['delete']))
{
unset($_SESSION['event_orders']);
}
else if(isset($_SESSION['event_orders']))
{
//displays delete link which redirects to same page with an additional request parameter as url?delete
echo "<a href='".$_SERVER['PHP_SELF']."?delete'>delete</a>";
}
else if(!in_array($_POST['event_id'], $_SESSION['event_orders'])) {
$_SESSION['event_orders'][] = $_POST['event_id'];
}
?>
修改强> 我们只需删除特定的数组项而不是完全取消数组。
<?php
session_start();
//if request parameter delete is present then unset the array
if(isset($_REQUEST['delete']))
{
unset($_SESSION['event_orders'][$_REQUEST['delete']]);
var_dump($_SESSION['event_orders']);
}
else if(in_array($_POST['event_id'], $_SESSION['event_orders']))
{
//displays delete link which redirects to same page with an additional request parameter as url?delete
$key = array_search($_POST['event_id'], $_SESSION['event_orders']);
var_dump($_SESSION['event_orders']);
echo "<a href='".$_SERVER['PHP_SELF']."?delete=".$key."'>delete</a>";
}
else
{
$_SESSION['event_orders'][] = $_POST['event_id'];
var_dump($_SESSION['event_orders']);
}
?>