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);
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With