Re: Fwd: Fwd: Re: goto - My comments

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 02:38 PM 12/27/2010, Jim Lucas wrote:
On 12/27/2010 10:42 AM, Ethan Rosenberg wrote:
<snip>

>
> Now, here is the real puzzler....
>
> The purpose of this routine is to be able to have two(2) forms on one page,but > not simultaneously.Additionally, l do not wish to call a separate program every > time a new form is used. The assumption is that the second form depends on the
> entries in the first form.  I realize this is not the case here.
>
> The age request and the kitten form both appear on the page together. How do I > accomplish having them appear separately? If it requires Java Script or jQuery,
> what is the code to be used?
>

> <snip>
>
>

The key is to look at the value of the submit button. This needs to be unique.

Change around your logic a little and you will have it.

<?php

// if form not yet submitted
// display form
if ( isset($_POST['submit']) && $_POST['submit'] === 'Submit' ) {
      // process form input
      // split date value into components
      $dateArr = explode('/', $_POST['dob']);

      // calculate timestamp corresponding to date value
      $dateTs = strtotime($_POST['dob']);

      // calculate timestamp corresponding to 'today'
      $now = strtotime('today');

      // check that the value entered is in the correct format
      if ( sizeof($dateArr) != 3 ) {
        die('ERROR: Please enter a valid date of birth');
      }

      // check that the value entered is a valid date
      if ( !checkdate($dateArr[0], $dateArr[1], $dateArr[2]) ) {
        die('ERROR: Please enter a valid date of birth');
      }

      // check that the date entered is earlier than 'today'
      if ( $dateTs >= $now ) {
        die('ERROR: Please enter a date of birth earlier than today');
      }

      // calculate difference between date of birth and today in days
      // convert to years
      // convert remaining days to months
      // print output
      $ageDays = floor(($now - $dateTs) / 86400);
      $ageYears = floor($ageDays / 365);
      $ageMonths = floor(($ageDays - ($ageYears * 365)) / 30);
echo "You are approximately $ageYears years and $ageMonths months old.";

} else if ( isset($_POST['submit']) && $_POST['submit'] === 'Submit Kitten' ) {

        $name_cat = $_POST['cat'];
        echo "Your Kitten is $name_cat";

} else {

    echo <<<HTML

<form method="post" action="agecalc3.php">
Enter your date of birth, in mm/dd/yyyy format: <br />
<input type="text" name="dob" />
<input type="submit" name="submit" value="Submit" />
</form>
<br /><br />
<form method="post" action="agecalc3.php">
Enter your kitten's name: <br />
<input type="text" name="cat" />
<input type="submit" name="submit" value="Submit Kitten" />
</form>

HTML;

}

?>

Jim Lucas


Jim -

Thanks.

Would you please look at the code you wrote again. I must have botched it, because both the age and kitten form still are on the same page. The age page should appear, the data should be accepted and then the kitten page should appear.

Ethan

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux