【问题标题】:Deleting specific div with PHP使用 PHP 删除特定的 div
【发布时间】:2015-01-10 10:50:33
【问题描述】:

我正在为学校作业制作一个用 PHP 做的应用程序。

此时,我可以添加新任务了。但问题是:

  • 当我单击一个 div(删除它)时,该 div 被删除,但所有其他任务都变得不可见。如果我创建一个新任务,它们只会再次出现。

这是我的代码

文件Index.php

<div class="container">

<form action="/Periodeopdracht/index.php" method="POST">
<div class="headerToDo">
    <input class="addText title" type="text" value="Click to add a task" name="nextToDo">
    <input class="clickablePlus" type="submit" value="+" name="submit"></div>
</form>

<?php if(!$empty): ?>
<?php foreach ($_SESSION["todoList"] as $_SESSION["key"] => $toDo): ?>
<div class="toDo">
    <form action="/Periodeopdracht/index.php" method="POST">

        <button value="<?php echo $_SESSION["key"] ?>" name="delete" class="delete" type="submit" >X</button>
        <button value="<?php echo $_SESSION["key"] ?>" name="done" class="done" type="submit" >V</button>
        <div value="<?php echo $_SESSION["key"] ?>" class="textToDo"><?= $toDo ?></div>
    </form>
</div>
<?php endforeach ?>
<?php endif ?>
</div>

应用程序.php

<?php
session_start();

$GLOBALS["empty"] = true;

$_SESSION['todoList'] = isset($_SESSION['todoList']) ? $_SESSION['todoList'] : array();


if(isset($_POST["submit"]))
{
    $empty = false;
    array_unshift($_SESSION['todoList'], $_POST["nextToDo"]);
}

if(isset($_POST["delete"]))
{
    unset($_SESSION['todoList'][$_SESSION["key"]]);
    //var_dump($key);
}

?>

【问题讨论】:

  • foreach ($_SESSION["todoList"] as $_SESSION["key"] =&gt; $toDo) 那行对我来说看起来很奇怪。每次迭代都为$_SESSION["key"] 分配一个新值。在执行结束时,$_SESSION["key"] 将包含最后看到的值。这是你想做的吗?你能告诉我们$_SESSION["key"]$_SESSION["todoList"] 应该包含什么吗?
  • '$_SESSION["key"]' 是每个 div 的唯一编号,因此我可以找出单击了哪个 div。 '$_SESSION["todoList"]' 应该包含所有添加的任务。

标签: php html arrays session


【解决方案1】:

尝试像普通帖子一样处理它,因为此密钥将在提交时可用:

<!-- no need to create every form -->
<form action="/Periodeopdracht/index.php" method="POST">
<?php foreach ($_SESSION["todoList"] as $key => $toDo): ?>
    <div class="toDo">
        <button value="<?php echo $key; ?>" name="delete" class="delete" type="submit" >X</button>
        <button value="<?php echo $key; ?>" name="done" class="done" type="submit" >V</button>
        <div value="<?php echo $key; ?>" class="textToDo"><?= $toDo ?></div>
    </div>
<?php endforeach; ?>
</form>

然后,在提交时:

if(isset($_POST["delete"])) {
    $key = $_POST["delete"];
    unset($_SESSION['todoList'][$key]);
}

旁注:您确定您的表单操作是/Periodeopdracht/index.php?也许这个操作打算使用Application.php,就像您在上面发布的$_POST 进程一样。

此外,这是多余的/不需要的。

$_SESSION['todoList'] = isset($_SESSION['todoList']) ? $_SESSION['todoList'] : array();

这种重新分配没有意义。

只需使用简单的初始化:

if(!isset($_SESSION['todoList'])) {
    $_SESSION['todoList'] = array();
}

一点点重写:

表格:

<div class="container">

    <form action="/Periodeopdracht/index.php" method="POST">
    <div class="headerToDo">
        <input class="addText title" type="text" value="Click to add a task" name="nextToDo">
        <input class="clickablePlus" type="submit" value="+" name="submit"></div>
    </form>

    <!-- no need to create every form -->
    <form action="/Periodeopdracht/index.php" method="POST">
        <?php foreach($_SESSION["todoList"] as $key => $toDo): ?>
            <div class="toDo">
                <button value="<?php echo $key; ?>" name="delete" class="delete" type="submit" >X</button>
                <button value="<?php echo $key; ?>" name="done" class="done" type="submit" >V</button>
                <div class="textToDo"><?php echo $toDo; ?></div>
            </div>
        <?php endforeach; ?>
    </form>

</div>

然后在/Periodeopdracht/index.php:

<?php
session_start();

// simple initialization
if(!isset($_SESSION['todoList'])) {
    $_SESSION['todoList'] = array();
}

// addition / push inside items
if(isset($_POST["submit"], $_POST["nextToDo"])) {
    $_SESSION['todoList'][] = $_POST["nextToDo"];
}

// delete key
if(isset($_POST["delete"])) {
    $key = $_POST["delete"];
    unset($_SESSION['todoList'][$key]);
}

?>

【讨论】:

  • 哦,我不知道你可以用 '$key = $_POST["delete"];'非常感谢您的帮助!
  • @RW24 是的,这将在您提交表单后可用。我很高兴这有帮助
  • 我不完全明白你的旁注是什么意思。
  • @RW24 我的旁注是指Application.php/Periodeopdracht/index.php 之间的冲突。我不知道您的问题中出现Application.php 的原因。
  • 因为有PHP逻辑。 (我在 index.php 中包含 application.php)这是错误的方式吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-24
  • 2015-11-10
  • 2020-07-13
  • 2020-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多