Posted by: garyfritz May 14 2012, 11:23 PM
I am an HTML/PHP idiot. I've dug around the docs but I don't see how to do this.
I have a simple file that prompts for a date, then sends it to a PHP script:
CODE
<form action="display.php" method="post">
<p>Enter date in the format YYMMDD: <input type="text" name="date" /></p>
<p> (E.g. 120205 for Feb. 5, 2012)</p>
<p><input type="submit" /></p>
</form>
display.php grabs the value and uses it:
CODE
<?php
$YYMMDD = htmlspecialchars($_POST['date']);
$split = str_split($YYMMDD, 2);
$YYsMMsDD = $split[0] . "/" . $split[1] . "/" . $split[2];
$YYYY = "20" . $split[0];
?>
I doped that much out on my own, and it works fine.
Now I would like to add a couple of buttons to the first file, so in addition to typing YYMMDD, I can alternately submit "today's date" or "yesterday's date" with a simple button push. I have no idea how to do that. Help?
Posted by: Brian Chandler May 15 2012, 12:31 AM
QUOTE
I doped that much out on my own, and it works fine.
OK, though I don't see what the htmlspecialchars() is for. The POST input should be a 6-digit number -- you should get in the habit *always* of checking input for validity (even if in this case there is no obvious malware danger), so just check with is_numeric().
One defect of html is that it doesn't really cater for inhomogenous inputs, such as "either select from an option list or select a radio button". Anyway, the simplest way is just to have buttons "Today", etc. If there really is no other input, these could be Submit buttons, with a name and value, which you just check in the script.
Another thing to remember is that concepts like "Today" are very wobbly on the web, since it's the *world wide* one. Probably my today at this instant is different from yours. So you had better explicitly write the date out in numbers on (or near) the button.
HTH
Posted by: garyfritz May 15 2012, 04:17 PM
Good points about validity checking and date issues. I'll fix it, but this is a small script for use by a few friends and family members so it's not a big concern.
Sounds like there is no way to have the HTML form accept EITHER an entered date OR a button click?
Ah! I figured out a solution. I added hyperlinks with ?parameters:
CODE
<h3>Click on <a href="display.php?today">TODAY</a> or <a href="display.php?yesterday">YESTERDAY</a> </h3>
<form action="display.php" method="post">
<p>Or enter date in the format YYMMDD: <input type="text" name="date" /></p>
<p> (E.g. 120205 for Feb. 5, 2012)</p>
<p><input type="submit" /></p>
</form>
Then in display.php I handled it so:
CODE
<?php
if (isset($_GET["today"])) {
$YYMMDD = date('ymd', strtotime('today'));
}
else {
if (isset($_GET["yesterday"])) {
$YYMMDD = date('ymd', strtotime('yesterday'));
}
else
{
if (is_numeric($_POST['date']))
$YYMMDD = $_POST['date'];
else
$YYMMDD = date('ymd', strtotime('today'));
}
}
$split = str_split($YYMMDD, 2);
$YYsMMsDD = $split[0] . "/" . $split[1] . "/" . $split[2];
$YYYY = "20" . $split[0];
?>
Maybe it's not elegant, but it works like a charm!