I'm trying to post the two inputs including the date, how to get the date and post it to the JSON file?
HTML
<form method="post" action="go.php">
Name:<input type="text" name="username">
Comment:<input type="text" name="comment">
<input type="submit" value="Submit">
PHP
<?php
$event = $_POST;
$filename = 'data.json';
$handle = @fopen($filename, 'r+');
if ($handle)
{
fseek($handle, 0, SEEK_END);
if (ftell($handle) > 0)
{
fseek($handle, -1, SEEK_END);
fwrite($handle, ',', 1);
fwrite($handle, json_encode($event) . ']');
}
else
{
fwrite($handle, json_encode(array($event)));
}
fclose($handle);
}
?>
There are multiple ways to send date in your current json:
You can send date in hidden fields from your html like :
<form method="post" action="go.php">
Name:<input type="text" name="username">
Comment:<input type="text" name="comment">
<input type="hidden" name="date" value="<?php echo date('yy-mm-dd');?>">
<input type="submit" value="Submit">
or
You can add date separately in your $event
variable.
$filename = 'data.json';
$handle = @fopen($filename, 'r+');
if ($handle)
{
fseek($handle, 0, SEEK_END);
if (ftell($handle) > 0)
{
fseek($handle, -1, SEEK_END);
fwrite($handle, ',', 1);
fwrite($handle, json_encode($event) . ']');
}
else
{
fwrite($handle, json_encode(array($event)));
}
fclose($handle);
}
?>
Well, it looks like I've figured out a better solution. Quite simple, really.
Just add a hidden input in HTML.
<input type="hidden" name="date">
And replace the $_POST value.
$_POST['date'] = date('Y-m-d H:i:s');
@MohdSayeed Thanks for your inspiration anyway.