Reputation: 67
This is a very basic project for a PHP class I am in - no databases involved so the data isn't going anywhere except the next page.
Currently, I am formatting the date on the page with the form using a hidden field, and it gets passed through in the URL post-formatting (with all the spaces punctuation). That satisfies the requirements of the assignment, but does not seem like the cleanest way to do this.
The form is "Comp2.php" and the page that displays the data is "Comp2b.php"
Here are the code segments in question:
(There are other fields in play of course, but I left them out since they are just text)
Comp2.php
<?php
if (isset($_POST['submit'])) {
$valid = true;
$dateadded = $_POST['dateadded'];
if ($valid) {
header("Location: Comp2b.php?albumid=$albumid&album=$album&artist=$artist&dateadded=$dateadded");
exit();
}
} else {
$albumid="";
$artist="";
$album="";
$price="";
$type="";
$playlists[0]="";
$genre="";
$tracks="";
}
?>
<form method="post" action="Comp2.php">
<?php $currentDate = date('l, F d, Y h:i:s a.') ?>
<input type="hidden" name="dateadded" value="<?php echo $currentDate; ?>">
Then the rest of the form continues...
This is Comp2b.php:
<?php
echo "Album ID: <strong>";
echo $_GET['albumid']."</strong><p><strong>".$_GET['album']."</strong> by <strong>".$_GET['artist']."</strong> added on ".$_GET['dateadded'];
?>
How can I pass something like date()
or time()
through the form, and do the formatting i.e., date('l, F d, Y h:i:s a.', $dateadded)
on my second page instead of passing a full string through the URL?
Upvotes: 0
Views: 2557
Reputation: 94662
In the first page put a simple time()
in the hidden field
<input type="hidden" name="dateadded" value="<?php echo time(); ?>">
Then in the second page output that timestamp formatted any way you like
<?php
echo "Album ID: <strong>";
echo $_POST['albumid'];
echo '</strong><p><strong>'.$_POST['album'];
echo '</strong> by <strong>' . $_POST['artist'];
echo ' </strong> added on ' . date('l, F d, Y h:i:s a.', $_POST['dateadded']);
?>
Reasons:
time() generates a simple number like 876243672834 which is much easier to pass around and less likely to cause confusion.
'date()` can have 2 parameter, the first is a format, and the second is a timestamp representing a date and time in unix format.
Also because your form
<form method="post"
has a method ofpost
the data will end up in the$_POST
array and not the$_GET
arrayThe
$_GET
array is populated when you do things like<a href="xxx.php?a=1&b=2">
or use<form method="GET"
Upvotes: 1