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

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

 



On 12/27/2010 10:42 AM, Ethan Rosenberg wrote:
> Jim -
> 
> Thank you ever so much.
> 
> At 01:58 PM 12/24/2010, you wrote:
> 
>> Here you are using two different arrays.  Yes, I know, they are basically the
>> same, but they are truly not the same.  In your case, use $_POST
>>
>> >>>> This is what I used.  As per your suggestion, I changed the == to ===.
>>> if(isset($_Request['Sex'])&& trim($_POST['Sex']) != '' )
>>> {
>>> if ($_REQUEST['Sex'] == "0")
>>> {
>>> $sex = 'Male';
>>> }
>>> else
>>> {
>>> $sex = 'Female';
>>> }
>>> }
>>
>> >>>> This defaults to Male.  I do not always search the Sex field.
>> if ( empty($_POST['Sex']) )
>> {
>>         $_POST['Sex'] = 'Male';
>> } else {
>>         $_POST['Sex'] = 'Female';
>> }
>>
>> +++++++++++
> 
> 
> 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?
> 
> Here is the code
> ==========
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
>    "DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
>   <head>
>     <title>Project 4-5: Age Calculator</title>
>   </head>
>   <body>
>     <h2>Project 4-5: Age Calculator</h2>
> <?php
>     // if form not yet submitted
>     // display form
> 
> 
> if (!isset($_POST['dob']))
>     {
> 
>         echo "  <form method=\"post\" action=\"agecalc3.php\">";
>         echo "  Enter your date of birth, in mm/dd/yyyy format: <br />";
>         echo "  <input type=\"text\" name=\"dob\" />";
>         echo "      <p>";
>         echo "  <input type=\"submit\" name=\"submit\" value=\"Submit\" />";
>         echo "  </form>";
> 
>         }
> else
>         {
>     // if form submitted
>     // 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.";
>  }
> 
> // SECOND FORM
> 
>  if (!isset($_POST['cat']))
>  {
> 
>         echo "  <form method=\"post\" action=\"agecalc3.php\">";
>         echo "  <br /><br />Enter your kitten's name: <br />";
>         echo "  <input type=\"text\" name=\"cat\" />";
>         echo "  <p>";
>         echo "      <input type=\"submit\" name=\"submit\" value=\"Submit
> Kitten\" />";
>         echo "    </form>";
> }
> else
> {
>         $name_cat = $_POST['cat'];
> 
>         echo "Your Kitten is $name_cat";
> }
> ?>
> 
>   </body>
> </html>
> ===============
> Thanks again.
> 
> Ethan
> 
> 
> 

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

-- 
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