The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> Buttons & inputs in a form
garyfritz
post May 14 2012, 11:23 PM
Post #1





Group: Members
Posts: 2
Joined: 14-May 12
Member No.: 17,111



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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
 
Reply to this topicStart new topic
Replies
garyfritz
post May 15 2012, 04:17 PM
Post #2





Group: Members
Posts: 2
Joined: 14-May 12
Member No.: 17,111



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!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Posts in this topic


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 25th April 2024 - 09:52 PM