The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> 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
Brian Chandler
post May 15 2012, 12:31 AM
Post #2


Jocular coder
********

Group: Members
Posts: 2,225
Joined: 31-August 06
Member No.: 43



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


--------------------
Brian Chandler
Nothing in this post constitutes "commercial solicitation". PayPal does not solicit residents of Japan. Contents may settle in transit. "Legal mind" may or may not be brain-damaged.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
garyfritz
post May 15 2012, 04:17 PM
Post #3





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

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: 1st October 2014 - 01:16 PM