PHP更新表太早

问题描述 投票:2回答:3

我有这个程序,其中用户输入的形式的消息和名称。然后程序保存在一个JSON文件信息。在JSON文件中的条目然后在表输出。

一切工作,因为我希望它只是当我添加表中的一个新条目。当我点击提交按钮表被用空条目进行更新。如果我再更新页面的表格获取输入了正确的价值观。

我已经看过了JSON对象被保存到文件和表单数据是否正确发送。从我的角度来看,这似乎是GET程序读取新进入前的表得到更新。不过,我真的不明白为什么,因为它是本地阵列$tempArray的foreach循环通过并执行时提交此数组会随着新的条目进行更新。

<?php
//open & read php file
$json = file_get_contents("../../../writeable/test2.json");
$tempArray = json_decode($json);

//get form values
if(isset($_POST['submit'])) {
    $name = $_POST['name'];
    $text = $_POST['text'];
    $date = date("m-d-Y h:i", time());
    //create new json object
    $new_post = array(
        'name' => $name,
        'text' => $text,
        'date' => $date,
        'ip' => $_SERVER['REMOTE_ADDR']
    );
    //add new json object to array
    $tempArray[] = $new_post;
    print_r($tempArray);
}

//encode array into json & save to file
$my_json = json_encode($tempArray);
$fh = fopen("../../../writeable/test2.json", 'wd') or die("can't open file");
fwrite($fh, $my_json);
fclose($fh);

?>

<!DOCTYPE html>
<html lang="sv-SE">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/style.css"/>
    <title>DT161G-Laboration1</title>
</head>
<body>
  <table id="guestbook">
          <?php
                    foreach ($tempArray as $obj) { //loop through array with posts
                        echo '<tr>';
                        echo '<td>' . $obj->name . '</td>';
                        echo '<td>' . $obj->text . '</td>';
                        echo '<td>' . 'IP: ' . $obj->ip . '</br>TID: ' . $obj->date . '</td>';
                        echo '</tr>';
                    }
                ?>
  </table>
 <form action="guestbook.php" method="POST">
            <input type="text" placeholder="Write your name"
                   name="name">
            <br>
            <label for="text">Your message</label>
            <textarea id="text" name="text"
                      rows="10" cols="50"
                      placeholder="Message .."></textarea>
            <br>
            <button type="submit" name ="submit">Send</button>
    </form>
</body>
php html
3个回答
1
投票

事情是json_decode($json)返回PHP的标准类对象,它是stdClass。在if块,你要追加与数组文本$tempArray对象。为了解决这个问题,你可以简单地传递在第二个参数truejson_decode

$tempArray = json_decode($json, true);

这将返回一个关联数组,这样就可以追加新阵列$tempArray。您也可以将强制转换到array作为object在其他的答案说。

顺便说一句,你加载甚至在新数据添加到该文件的JSON数据,你也正在试图即使窗体不会提交添加数据。但实际上,你又重新加入现有的数据不提交表单时。这是我对你的代码所做的更改。

<?php

//open & read php file
$json = file_get_contents("../../../writeable/test2.json");
$tempArray = json_decode($json, true);

//get form values
if(isset($_POST['submit'])) {
    $name = $_POST['name'];
    $text = $_POST['text'];
    $date = date("m-d-Y h:i", time());
    //create new json object
    $new_post = array(
        'name' => $name,
        'text' => $text,
        'date' => $date,
        'ip' => $_SERVER['REMOTE_ADDR']
    );
    //add new json object to array
    $tempArray[] = $new_post;
    print_r($tempArray);

    //encode array into json & save to file
    $my_json = json_encode($tempArray);
    $fh = fopen("../../../writeable/test2.json", 'wd') or die("can't open file");
    fwrite($fh, $my_json);
    fclose($fh);
}

//open & read php file
$json = file_get_contents("../../../writeable/test2.json");
$tempArray = json_decode($json);

?>

3
投票

JavaScript没有关联数组。因此,这样的PHP数组被转换成对象。您从JSON文件中读取对象,但附加一个关联数组。

在循环中的所有工作,除了采用最新加入阵列的罚款。 $obj->name不能访问数组元素$obj['name']

通过改变你的代码与对象句柄

$new_post = (object)[
  'name' => $name,
  'text' => $text,
  'date' => $date,
  'ip'   => $_SERVER['REMOTE_ADDR'],
];

2
投票

从我的角度来看,这似乎是GET程序读取新进入前的表得到更新。

没有!

  1. 你所面对的问题是因为,当你从文件中获取JSON数据并使用json_decode解码它,你有对象的数组,如果表单提交您插入一个数组对象的数组。 $new_post = array( 'name' => $name, 'text' => $text, 'date' => $date, 'ip' => $_SERVER['REMOTE_ADDR'] ); //add new json object to array $tempArray[] = $new_post;
  2. 然后在你的循环中,您所访问的元素作为对象e.g $obj->name;

最简单的办法

最简单的解决方法是TYPE-投你$new_post数组喜欢的对象:

$new_post =(object) array(
    'name' => $name,
    'text' => $text,
    'date' => $date,
    'ip' => $_SERVER['REMOTE_ADDR']
);

你可以阅读更多关于铸造Here

© www.soinside.com 2019 - 2024. All rights reserved.