Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Forms and remembering values help

    Hello:

    I was hoping that someone can help me out.

    I have a form which asks for three pieces of information: purchase price, planned closing date and location.

    When the person hits the Submit button, they are brought to the next screen which goes through some calculations, etc.

    If the results are not favorable, I would like the person to click the Back button and return to this first form and be able to make changes. When the Back button is clicked, I would like for the information which was entered to be displayed. I don't want the person to re-enter everything.

    I was able to get the purchase price and location fields to work with no problem. The trouble I have is with the planned closing date. I am unable to figure out how to maintain that information.

    I do have sessions turned on in a file called header.html which is included in the script. I tried a number of things and I keep getting syntax errors.

    Can someone help me out with remembering the date? Thank you for the help.

    This is the script.
    Code:
    <?php
    
    include('header.html');
    
    ?>
    
    <form action="calc-best-down-pmt.php" method="post">
    <table class="apptable">
    <tr><td class="a"><b>Purchase Price:</b></td><td class="b"><input type="text" name="purchaseprice" id="purchaseprice" class="txt" size="45" value="<?php if (isset($_SESSION['purchaseprice'])) echo $_SESSION['purchaseprice']; ?>"></td></tr>
    <tr><td class="a"><b>Planned Closing Date:</b></td>
        <td class="b">
           <?php
              $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
                    'September', 'October', 'November', 'December');
    
                //Make the days and years arrays
    
               $days = range (1,31);
               $years = range (2007, 2015);
    
               //Make the months pull-down menu
    
               echo '<select name="month">';
               foreach ($months as $key => $value) {
               echo "<option value=\"$key\">$value</option>\n";
                  }
               echo '</select>';
    
               //Make the days pull-down menu
    
               echo '<select name="day">';
               for ($day = 1; $day <= 31; $day++) {
               echo "<option value=\"$day\">$day</option>\n";
                }
               echo '</select>';
    
              //Make the years pull down menu
    
              echo '<select name="year">';
              $year = 2007;
              while ($year <= 2015) {
              echo "<option value=\"$year\">$year</option>\n";
              $year++;
                }
              echo '</select>';
    
    ?>
    <tr><td class="a"><b>Property Location:</b></td>
        <td class="b"><select name="location" size=1>
     <?php    
            if(isset($_SESSION['location']) && $_SESSION['location'] == 'Georgia')
           echo '<option value="Georgia" selected> Georgia&nbsp;';
           else
           echo '<option value="Georgia">Georgia&nbsp;';
    ?>              </select></td></tr>
    <tr><td>&nbsp;</td></tr>
    </table>
    <input type="submit" name="btnSubmit" id="btnSubmit" value="Calculate Best Down Payment Options and Typical Closing Costs" class="btn" >
    <input type="hidden" name="submitted" value="TRUE" />
    
    </form>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    I sort of hammered out some changes ... but it's just a starting point.

    You basically only have 1 script. It displays the form and then the form
    calls itself. It re-displays the form, but this time there were variables
    coming in that were defined, so it does the calculation, and displays the
    form again still showing their variables.

    This is sort of the idea, but again, not totally complete (and not tested).

    PHP Code:
    <?php

    $p
    =$_POST['purchaseprice'];
    $m=$_POST['month'];
    $d=$_POST['day'];
    $y=$_POST['year'];
    $l=$_POST['location'];

    // calculate everything here if a month variable exists.
    if($_POST['month'];){
    //calculations here
    // $result= whatever the result is
    }

    include(
    'header.html');

    ?>

    if($result){
    // display the calculations and then simply display the form
    // underneath with the information they put in.
    }

    <form action="scriptname.php" method="post">
    <table class="apptable">
    <tr><td class="a"><b>Purchase Price:</b></td><td class="b"><input type="text" name="purchaseprice" id="purchaseprice" class="txt" size="45" value="<?=$p?>"></td></tr>
    <tr><td class="a"><b>Planned Closing Date:</b></td>
        <td class="b">
           <?php

              $months 
    = array (=> 'January''February''March''April''May''June''July''August',
                    
    'September''October''November''December');

                
    //Make the days and years arrays

               
    $days range (1,31);
               
    $years range (20072015);

               
    //Make the months pull-down menu

    if($_POST['month']){
    // for each of these  month, day year ...
    // check for the $_POST variable and make that SELECTED.
    // example:  <option selected>$m</option>
    // I show that for "month", but do it for day and year also.
    }
               echo 
    '<select name="month">';
               if(
    $_POST['month']){
               echo 
    "<option selected>$m</option>\n";
               }
               foreach (
    $months as $key => $value) {
               echo 
    "<option value=\"$key\">$value</option>\n";
                  }
               echo 
    '</select>';

               
    //Make the days pull-down menu

               
    echo '<select name="day">';
               for (
    $day 1$day <= 31$day++) {
               echo 
    "<option value=\"$day\">$day</option>\n";
                }
               echo 
    '</select>';

              
    //Make the years pull down menu

              
    echo '<select name="year">';
              
    $year 2007;
              while (
    $year <= 2015) {
              echo 
    "<option value=\"$year\">$year</option>\n";
              
    $year++;
                }
              echo 
    '</select>';

    ?>
    <tr><td class="a"><b>Property Location:</b></td>
        <td class="b"><select name="location" size=1>
     <?php    
            
    if(isset($_SESSION['location']) && $_SESSION['location'] == 'Georgia')
           echo 
    '<option value="Georgia" selected> Georgia&nbsp;';
           else
           echo 
    '<option value="Georgia">Georgia&nbsp;';
    ?>              </select></td></tr>
    <tr><td>&nbsp;</td></tr>
    </table>
    <input type="submit" name="btnSubmit" id="btnSubmit" value="Calculate Best Down Payment Options and Typical Closing Costs" class="btn" >
    <input type="hidden" name="submitted" value="TRUE" />

    </form>
    Last edited by mlseim; 08-17-2007 at 07:55 PM.

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You do this similar to how you bring back the other field data. Your date is stored via select tags... as you build each select tag, evaluate the $_SESSION variable and when that value matches the option value then add echo "selected" within the option tag. I tend to prefer the shortcut "if" statement syntax (I forget what it's called), the "Excel" technique of (condition ? true : false).

    For example:

    PHP Code:
    echo '<select name="day">';
    for (
    $day 1$day <= 31$day++) {
    echo 
    "<option value=\"$day\"".($_SESSION['day'] == $day "selected" "").">$day</option>\n";
                }
               echo 
    '</select>'

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by Fumigator View Post
    I tend to prefer the shortcut "if" statement syntax (I forget what it's called), the "Excel" technique of (condition ? true : false).
    The ternary operator.

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    When using sessions, don't forget this at the top of your script:

    <?php
    session_start();


    ... and other thing ...

    Your application may be a perfect project for AJAX.
    A good way to learn some AJAX stuff.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •