简体   繁体   English

PHP Todo CheckList检查的功能

[英]PHP Todo CheckList Checked Feature

I need help with a PHP ToDo List. 我需要有关PHP待办事项列表的帮助。 I have most of it working except for the check box to move it from a completed list to a still need to do list. 除了将其从已完成列表移至仍需要执行的复选框之前,我大部分工作都在进行。 here is the code I have so far: 这是我到目前为止的代码:

For the edit screen: 对于编辑屏幕:

%% views/header.html %%
        <h1>{{$title}}</h1>
        <div class='inputs'>
        <form action="@@todo/update@@" method="post">
            <input type="hidden" id="id" name="id" value="{{$todo['id']}}" />
            <label for="description">Description:</label>
            <input type="text" id="description" name="description" value="{{$todo    ['description']}}" />
            <label for="done">Done?:</label>
            <input type="checkbox" id="done" name="done" value="1" />
            <input type="submit" value="Update" />
        <form>
    </div>
    <p><a href="@@index@@"><< Back</a></p>
    %% views/footer.html %%

For the todo.inc file: 对于todo.inc文件:

<?php
include_once "include/util.inc";
include_once "models/todo.inc";
function safeParam($arr, $index, $default) {
    if ($arr && isset($arr[$index])) {
        return $arr[$index];
    }
    return $default;
}
function get_view($params) {
    $id = safeParam($params, 0, false);
    if ($id === false) {
        die("No todo id specified");
    }
    $todo = findToDoById($id);
    if (!$todo) {
        die("No todo with id $id found.");
    }
    // @formatter:off
    renderTemplate(
        "views/todo_view.inc",
        array(
            'title' => 'Viewing To Do',
            'todo' => $todo
        )
    );
    // @formatter:on
}
function get_list($params) {
    $todos = findAllCurrentToDos();
    $dones = findAllDoneToDos();
    // @formatter:off
    renderTemplate(
        "views/index.inc",
        array(
            'title' => 'To Do List',
            'todos' => $todos,
            'dones' => $dones
        )
    );
    // @formatter:on
}
function get_edit($params) {
    $id = safeParam($params, 0, false);
    if (!$id) {
        die("No todo specified");
    }
    $todo = findToDoById($id);
    if (!$todo) {
        die("No todo found.");
    }
    // @formatter:off
    renderTemplate(
        "views/todo_edit.inc",
        array(
            'title' => 'Editing To Do',
            'todo' => $todo
        )
    );
    // @formatter:on
}

function post_add($params) {
    if (!isset($_POST['description'])) {
        die("no description given");
    }
    $description = htmlentities($_POST['description']);
    addToDo($description);
    redirectRelative("index");
}

function validate_present($elements) {
    $errors = '';
    foreach ($elements as $element) {
        if (!isset($_POST[$element])) {
            $errors .= "Missing $element\n";
        }
    }
    return $errors;
}

function post_update($params) {
    $errors = validate_present(array('id', 'description', 'done'));
    if ($errors) {
        die($errors);
    }
    $id = $_POST['id'];
    $description = $_POST['description'];
    $done = $_POST['done'];
    updateToDo($id, $description, $done);
    redirectRelative("todo/view/$id");
    }

function get_delete($params) {
    $id = safeParam($params, 0, false);
    if (!$id) {
        die("No todo specified");
    }
    $todo = findToDoById($id);
    if (!$todo) {
        die("No todo found.");
    }
    deleteToDo($id);
    redirectRelative("index");
}
?>

Your error is in these two functions. 您的错误在于这两个功能。

function validate_present($elements) {
    $errors = '';
    foreach ($elements as $element) {
        if (!isset($_POST[$element])) {
            $errors .= "Missing $element\n";
        }
    }
    return $errors;
}

function post_update($params) {
    $errors = validate_present(array('id', 'description', 'done'));
    if ($errors) {
        die($errors);
    }
    $id = $_POST['id'];
    $description = $_POST['description'];
    $done = $_POST['done'];
    updateToDo($id, $description, $done);
    redirectRelative("todo/view/$id");
}

You are attempting to validate that done exists in validate_present when called by post_update . 您正在尝试验证done在存在validate_present时称为post_update done obviously cannot exists since it is not sent to the server when the checkbox is not checked. 显然不存在done ,因为未选中该复选框时,它不会发送到服务器。 The $_POST does not even contain that variable, so it returns that element is missing (since it technically is). $_POST甚至不包含该变量,因此它返回缺少的元素(因为从技术上讲是这样)。 I would leave validate_present alone and change post_update as follows: 我将不留下validate_present post_update以下方式更改post_update

function post_update($params) {
    $errors = validate_present(array('id', 'description'));
    if ($errors) {
        die($errors);
    }
    $id = $_POST['id'];
    $description = $_POST['description'];
    $done = (isset($_POST['done'])? 1 : 0);
    updateToDo($id, $description, $done);
    redirectRelative("todo/view/$id");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM