Click to See Complete Forum and Search --> : Form and Remembering values help


focus310
08-17-2007, 11:15 AM
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>

TJ111
08-17-2007, 12:34 PM
Although I'm not entirely sold on your methods, using your scripts it wouldn't be hard to do.

echo '<select name="month">';
foreach ($months as $key => $value) {
if ($_SESSION['month'] == $value) {
echo "<option value=\"$key\" selected='selected'>$value</option>\n";
} else {
echo "<option value=\"$key\">$value</option>\n";
}
}
echo '</select>';

bubbisthedog
08-17-2007, 12:36 PM
Untested, but you should get the idear.

echo '<select name="month">';
foreach ($months as $key => $value) {
echo "<option <?php if ($POST_['month'] == $value) {echo 'selected="selected"';} ?> value=\"$key\">$value</option>\n";
}
echo '</select>';

bubbisthedog
08-17-2007, 01:22 PM
Untested, but you should get the idear.

echo '<select name="month">';
foreach ($months as $key => $value) {
echo "<option <?php if ($POST_['month'] == $value) {echo 'selected="selected"';} ?> value=\"$key\">$value</option>\n";
}
echo '</select>';

Man, I hammered that. :D I must fix it:

echo '<select name="month">';
foreach ($months as $key => $value) {
if ($POST_['month'] == $value) {
$selected = "selected"
}
echo '<option $selected value=\"$key\">$value</option>';
$selected = '';
}
echo '</select>';

Still may need a little clean-up, but at least it's not the atrocity that I posted earlier. :D

focus310
08-17-2007, 02:55 PM
Hi,

Thanks for the reply.

I tried the code and when I ran the script I received the following:
Parse error: syntax error, unexpected '}' in C:\Inetpub\fullfocus\AtlantaMtgPlace\best-down-pmt-close-costs.php on line 27

It doesn't like one of the brackets. I tried removing and shifting the bracket to other locations and the result was the same.

focus310
08-17-2007, 02:56 PM
This is a reply to TJ111. I tried your syntax and when I clicked the Back button, the month defaulted January instead of keeping the month I actually selected.

TJ111
08-17-2007, 03:07 PM
Put a semicolon ";" after "selected"

$selected = "selected";


Edit:That's referring to bubbis's code.


echo '<select name="month">';
foreach ($months as $key => $value) {
if ($POST_['month'] == $value) {
$selected = "selected=\\'selected\\'";
}
echo '<option $selected value=\"$key\">$value</option>';
unset($selected);
}
echo '</select>';

Try that

focus310
08-17-2007, 03:15 PM
I added the ; after selected. I ran the script and my drop down box changed all the month values (January, February, etc) to $value. When I click on the drop down box, I have $value appearing 12 times.

TJ111
08-17-2007, 03:17 PM
echo '<select name="month">';
foreach ($months as $key => $value) {
if ($POST_['month'] == $value) {
$selected = "selected='selected'";
}
echo "<option $selected value='$key'>".$value."</option>";
unset($selected);
}
echo '</select>';

focus310
08-17-2007, 03:25 PM
Well, I made the changes. The name of the months appear. I run the script and when I click the Back button, the month shown is January not the month I selected.

TJ111
08-17-2007, 03:31 PM
Show me the script you use to define the sessions.

focus310
08-17-2007, 03:41 PM
<?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
$_SESSION['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>

TJ111
08-17-2007, 03:42 PM
No where you define the $_SESSION superglobal parameters.

bubbisthedog
08-17-2007, 03:44 PM
Sorry about the mistakes. :o Thanks for cleaning it up, TJ (sort of mad at myself for not putting the XHTML 1.0 Strict-correct selected="selected").

By the way, TJ, which approach do you think is 'better,' i.e. more efficient, etc., mine ($selected) or yours (use if...then logic to write out the <option>)? (Not intended to be an ego-booster; I'm just trying to get better. :) )

TJ111
08-17-2007, 03:56 PM
I ran both statements through PhpED's profiler, and here's what it gave me.

For the if...else way :0.129 ms
For the $selected way: 0.130 ms

So officially speaking, my way is .0000001 seconds faster :) whatever that counts for.

bubbisthedog
08-18-2007, 06:49 AM
:D Okay, then. Thanks, TJ.

bokeh
08-18-2007, 09:59 AM
which approach do you think is 'better,' i.e. more efficient, etc., mine ($selected) or yours (use if...then logic to write out the <option>)? (Not intended to be an ego-booster; I'm just trying to get better. :) )Neither way is good. Yours raises an E_ALL level notice and the other method involves unnecessarily writing to a variable on every iteration of the loop.

bubbisthedog
08-21-2007, 02:15 PM
Yikes. bokeh, what approach would you suggest to write out these <select> options? I ask because I use my approach all the time. :o

(MOD: If you think I'm hijacking this thread, please let me know and I'll start a new one.)