...

View Full Version : Forms and remembering values help



focus310
08-17-2007, 05:19 PM
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.


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

mlseim
08-17-2007, 06:46 PM
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

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

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>

Fumigator
08-17-2007, 06:46 PM
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:



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

Inigoesdr
08-17-2007, 07:54 PM
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.

mlseim
08-17-2007, 11:01 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum