我如何添加到一个列表通过输入Post在html, json和php?(不使用数据库)?



我已经为此挣扎了一段时间了。我觉得我的逻辑中缺少了一块,我不知道它是什么。我试图使一个todo应用程序使用Html, json和php。我如何使输入值存储为数组,然后作为待办事项列表回显?

<form method="POST" action="TodoAppChallenge.php">
<p>
<label for="wordsGoHere">type your task here</label>
<input type="text"  id ="wordsGoHere" name="wordsGoHere">
</p>
<input type="submit" name="pressHere" value="           add Your Task               ">
</form>
<?php
include('TodoAppChallengeExtra.php');
?>
<table>
<thead>
<th>Your Tasks</th>
</thead>
<tbody>
<?php
if(isset($_POST['pressHere'])){

$wordsGoHere = array(
'wordsGoHere' => $_POST['wordsGoHere'],
);    //i think my mistake is somewhere around here
array_push($wordsGoHere,$_POST['wordsGoHere']);
$data = $wordsGoHere;
$data = json_encode($data, JSON_PRETTY_PRINT);
file_put_contents('extra.json', $data);
$data = file_get_contents('extra.json');
$data = json_decode($data);
foreach($data as $row){
echo "
<tr>
<td>
<ul><li>".$row->$data."</li></ul>
</td>
</tr>
";
}} else {
echo 'add a Task';
}
?>
</tbody>
</table>

代码有注释,但这里是要点:

  • 将表单处理移到顶部以避免,"Headers已经发送…"错误
  • 打开错误报告
  • 使用相同的、纯语言的变量名
  • 让你的数组尽可能的简单

您还可以做些什么来改进?使用常量来定义会话密钥todo、验证等内容,并将其拆分为处理文件和演示文件。

如果你想更高级一点,设计一个存储机制,你的表单和表单处理逻辑不知道或关心数据是否存储在JSON文件、会话、数据库或其他地方。它看起来像:$storage->save($userInput);$storage->retrieve();

祝你好运!

通过session实现。

<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// Always do these 2 things on every request.
// 1. Start your session
session_start();
// 2. Initialize the session array if it hasn't been initialized already.
// This only happens once per session.
if (!isset($_SESSION['todo'])) {
$_SESSION['todo'] = [];
}
// Now you are free to start writing and reading from your session.
// Check if the request is coming in via POST.
// Do your processing up top above your HTML otherwise you can run into "Headers already sent..." errors.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// This is what the user typed in.
$input = $_POST['wordsGoHere'];
// @todo do your validation here.
// Put the user's input in the session.
$_SESSION['todo'][] = $input;
}
// Finally, read out the contents from your session so your form has access to it.
$dataFromSession = $_SESSION['todo'];
?>
<form method="POST">
<p>
<label for="wordsGoHere">type your task here</label>
<input type="text" id="wordsGoHere" name="wordsGoHere">
</p>
<input type="submit" name="submit" value="Add Your Task">
</form>
<table>
<thead>
<tr>
<th>
Your Tasks
</th>
</tr>
</thead>
<tbody>
<?php foreach ($dataFromSession as $item): ?>
<tr>
<td>
<?= $item; ?>
</td>
</tr>
<? endforeach; ?>
</tbody>
</table>

通过JSON文件实现。

<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// Initialize your JSON file by ensuring the file actually exists. If it doesn't, create an empty file.
if (!file_exists('extra.json')) {
file_put_contents('extra.json', '[]');
}
// Now you are free to start writing and reading from your JSON file.
// Check if the request is coming in via POST.
// Do your processing up top above your HTML otherwise you can run into "Headers already sent..." errors.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// This is what the user typed in.
$input = $_POST['wordsGoHere'];
// @todo do your validation here.
// Read the current state of the file.
$contents = file_get_contents('extra.json');
$dataFromFile = json_decode($contents, true);
// Push the new value on the end of the array.
$dataFromFile[] = $input;
// Write the array back to the file.
$json = json_encode($dataFromFile, JSON_PRETTY_PRINT);
file_put_contents('extra.json', $json);
}
// Finally, read out the latest contents from your file so your form has access to it.
$contents = file_get_contents('extra.json');
$dataFromFile = json_decode($contents, true);
?>
<form method="POST">
<p>
<label for="wordsGoHere">type your task here</label>
<input type="text" id="wordsGoHere" name="wordsGoHere">
</p>
<input type="submit" name="submit" value="Add Your Task">
</form>
<table>
<thead>
<tr>
<th>
Your Tasks
</th>
</tr>
</thead>
<tbody>
<?php foreach ($dataFromFile as $item): ?>
<tr>
<td>
<?= $item; ?>
</td>
</tr>
<? endforeach; ?>
</tbody>
</table>

我试图使它更简单,你有很多变量重新赋值,你不需要。基本上

if(isset($_POST['pressHere'])){
// get the tasks, note the 'true' flag on the json decode so you have an array rather than an object
$tasks = json_decode(file_get_contents('extra.json'), true);

//keep things simple - use the same array and just add the new value to it
$tasks[] = $_POST['wordsGoHere'];
//print out your values
foreach($tasks as $task){
echo "
<tr>
<td>
<ul><li>" . $task . "</li></ul>
</td>
</tr>
";
}
//using the same array prepare to save it
$tasks = json_encode($tasks, JSON_PRETTY_PRINT);
//save the data
file_put_contents('extra.json', $tasks);

} else {
echo 'add a Task';
}

它更容易阅读,易于遵循,有意义,(重要的是)工作

//编辑值得一提的是,我只是遵循了你的逻辑,所以列表只在你发布的时候显示,如果你想让它一直显示,你只需要稍微改变一下逻辑,就像这样:

$tasks = json_decode(file_get_contents('extra.json'), true);
if(isset($_POST['pressHere'])) {
$tasks[] = $_POST['wordsGoHere'];
}
if(count($tasks) > 0) {
foreach($tasks ....
....
} else {
echo 'add a Task';
}

最新更新