Click to See Complete Forum and Search --> : PHP Sessions and Input Tags


peteyb
10-15-2007, 07:25 AM
Hi all

I use the following code to declare all the posted variables within my form to the session array:


<?php
session_start();
foreach($_POST as $key => $value)
{
$_SESSION[$key] = $value;
}
?>


Whilst this stores the data in the array I then have to use a the following code on each individual input tag to make the browser display the selected option when a user returns to the page.



<select id='value_claim3' name='value_claim3' size='1'>
<option value="0" <?php if(isset($_SESSION['value_claim3']) && $_SESSION['value_claim3'] == '0') { ?>selected="selected"<? } ?>></option>
<option value=250 <?php if(isset($_SESSION['value_claim3']) && $_SESSION['value_claim3'] == '250') { ?>selected="selected"<? } ?>>£0 - £500</option>
<option value=750 <?php if(isset($_SESSION['value_claim3']) && $_SESSION['value_claim3'] == '750') { ?>selected="selected"<? } ?>>£501 - £1,000</option>
<option value=1250 <?php if(isset($_SESSION['value_claim3']) && $_SESSION['value_claim3'] == '1250') { ?>selected="selected"<? } ?>>£1,001 - £1,500</option>
<option value=1750 <?php if(isset($_SESSION['value_claim3']) && $_SESSION['value_claim3'] == '1750') { ?>selected="selected"<? } ?>>£1,501 - £2,000</option>
<option value=2001 <?php if(isset($_SESSION['value_claim3']) && $_SESSION['value_claim3'] == '2001') { ?>selected="selected"<? } ?>>Over £2000</option>
</select>


Is there a short bit of code similar to the referencing method shown first whereby the browser will display the selected option?

valenok
10-15-2007, 03:14 PM
for ($i=250;$i<1500;$i=($i+250)){
echo "<option value='".$i."'".
if($i==$abc) echo " selected=\"selected\" ";
echo ">"
}

btw -> check input data, ot at least apply smth. like intval()

NightShift58
10-15-2007, 03:24 PM
<?php
IF (isset($_SESSION['value_claim3'])) :
$claim_val = $_SESSION['value_claim3'];
ELSE :
$claim_val = FALSE;
ENDIF;

$arrLEVEL = array();
$arrLEVEL[ 250] = "£0 - £500";
$arrLEVEL[ 750] = "£501 - £1000";
$arrLEVEL[1250] = "£1001 - £1500";
$arrLEVEL[1750] = "£1501 - £2000";
$arrLEVEL[2001] = "over £2000";

echo "<select id='value_claim3' name='value_claim3' size='1'>\n";
FOREACH ($arrLEVEL as $thisLEVEL => $thisTEXT) :
echo "<option value='$thisLEVEL'" . ($claim_val==$thisLEVEL ? " selected='selected'" : "") . ">" . $thisTEXT . "</option>\n";
ENDFOREACH;
echo "</select>\n";
?>