我在PHP中为学校作业做了一个待办事项。
此时,我可以添加新任务。但问题是:
这是我的代码
档案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>
Application.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);
}
?>
答案 0 :(得分:2)
尝试像普通帖子一样处理它,因为此密钥将在提交时提供:
<!-- 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]);
}
?>