Forum Moderators: coopster

Message Too Old, No Replies

retaining html form data using php sessions

php session,retain form data

         

NeilsPHP

3:38 am on Aug 20, 2008 (gmt 0)

10+ Year Member



When the form is loaded for the second time, i.e, I validate the datas in page 1 and if error in validation,error message is displayed, it is reloaded after pressing back button(by user). While doing this I want the values entered by the user has to be retained so they don't have to enter again..I can do this for text fields, without any problem using sessions but I don't know how to retain for a selection box, and option buttons.I can use name-value pair for others,but not for selection boxes.. Please help Thanks

eelixduppy

6:02 am on Aug 20, 2008 (gmt 0)



You have to check if the value matches the value within the selection. For example:

<option value="one" <?php echo (isset($_SESSION['sel']) && $_SESSION['sel'] == 'one') ? 'selected' : ''; ?>>One</option>
<option value="two" <?php echo (isset($_SESSION['sel']) && $_SESSION['sel'] == 'two') ? 'selected' : ''; ?>>Two</option>
etc....

This uses the ternary operator for brevity. Hope this helps.

cameraman

6:02 am on Aug 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The php needs to produce html for a browser to understand. Think of a select:
<select name="select">
<option value="1">a</option>
<option value="2" selected>b</option>
<option value="3">c</option>
</select>

Assuming you've gotten the value from the session back into variable $select, you would:
<select name="select">
<option value="1"<?php if($select == 1) echo " selected"; ?>>a</option>
<option value="2"<?php if($select == 2) echo " selected"; ?>>b</option>
<option value="3"<?php if($select == 3) echo " selected"; ?>>c</option>
</select>

Use the same method for check boxes and radio buttons.
<input type="checkbox" name="check" value="1"<?php if($check == 1) echo " checked"; ?>>

Yeesh I hit 'refresh' before posting and still get beaten ;)

NeilsPHP

1:26 am on Aug 21, 2008 (gmt 0)

10+ Year Member



Thanks for reply folks,but somehow i can't get it to work.here is my code

"checkform.php"

<?php session_start();?>

¦
¦html header here
¦
<?php
if (isset($_POST['submit']))
{

$_SESSION['Variable']= $_POST['Variable'];

////validation & other code here...

}
else
{include("form.php");}
?>

"form.php"

///I do not have session start here becoz its already in checkform.php and variable seem to get transferred on back and forth submit form..so sessions are working fine

<SELECT NAME="Variable">

<OPTION VALUE="nothing1"<?php if($_SESSION[''] == nothing1){ echo "";} ?>></option> //////this line to get top selection empty

<OPTION VALUE="A"<?php if($_SESSION['Variable'] =="A"){ echo "A";} ?>>A</option>
<OPTION VALUE="B"<?php if($_SESSION['Variable'] =="B"){ echo "B";} ?>>B</option>

?>
if I echo Variable anywhere in page,its showing fine..but not inside the selection box..
what is it that i m missing ?
thanks in adv

cameraman

5:46 am on Aug 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do not have session start here becoz
If it's a different script, you need it.

<OPTION VALUE="nothing1"<?php if($_SESSION[''] == nothing1){ echo "";} ?>></option> //////this line
This will never be true so you'll never get anything out of it.

<OPTION VALUE="A"<?php if($_SESSION['Variable'] =="A"){ echo "A";} ?>>A</option>
This should be:
<OPTION VALUE="A"<?php if($_SESSION['Variable'] =="A"){ echo " selected";} ?>>A</option>

NeilsPHP

2:40 pm on Aug 24, 2008 (gmt 0)

10+ Year Member



worked fine this time..thank u so much