Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I post date in PHP?

Tags:

php

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);
}
?>
like image 317
skygate Avatar asked Jun 30 '26 15:06

skygate


1 Answers

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);
}
?>
like image 194
Mohammad Sayeed Avatar answered Jul 02 '26 06:07

Mohammad Sayeed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!