0

我在页面上有一个按钮,它是一个简单的输入提交按钮,它将页面 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;
} ?>
4

2 回答 2

0

您可以使用未设置功能。参考: http: //php.net/manual/en/function.unset.php

于 2012-06-29T10:08:52.307 回答
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']);
}
?>
于 2012-06-29T10:12:18.610 回答