Jump to content

Help with session variables needed


garyed

Recommended Posts

I have a php page that has about 50 separate data entries that I want to have the user be able to leave the page & return without losing all the data already entered. I'm assuming I would need to use session variables to make that happen so someone please correct me if I'm wrong. Below is a sample of the the code I'm using for just one variable. 

<?php 
/* Start session & put variables into session so the user can leave page & return without losing everything */
session_start();
if ($_POST['windows']!="") { $_SESSION['windows']=$_POST['windows'];}
?>
<form method="POST" action="">
<table> <tbody>
<tr>
  <td style="text-align: right;">
 Windows   </td>
     <td><input name="windows" style="background-color:#FFFFCC;width:60px;" maxlength="10"  value="<?php if (isset($_SESSION['windows'])){ echo $_SESSION['windows'];}else{echo $_POST['windows'];} ?>"type="text">
   
 </td> </tr>
 <tr><td> <input type="submit" value="Calculate"></td></tr> </tbody></table>


 </form>

All the data entered is then used for calculations so my questions are:

1 - Is there an easier way to do this without repeating the code for all 50 separate variables?

2 - Once data is entered into a field it still comes back even if I empty out the field.

 

Any help appreciated

Link to comment
https://forums.phpfreaks.com/topic/278151-help-with-session-variables-needed/
Share on other sites

  On 5/19/2013 at 1:02 AM, garyed said:

I have a php page that has about 50 separate data entries that I want to have the user be able to leave the page & return without losing all the data already entered. I'm assuming I would need to use session variables to make that happen so someone please correct me if I'm wrong. Below is a sample of the the code I'm using for just one variable. 

 

1 - Is there an easier way to do this without repeating the code for all 50 separate variables?

 

Any help appreciated

 

Use function

function get_value($var)
{
	return (isset($_SESSION[$var]) && !empty($_SESSION[$var])) ? $_SESION[$var] : $_POST[$var];
}

// then on your html
<td><input name="windows" style="background-color:#FFFFCC;width:60px;" maxlength="10"  value="<?php echo get_value('windows'); ?>"type="text">
<td><input name="other_input" style="background-color:#FFFFCC;width:60px;" maxlength="10"  value="<?php echo get_value('other_input'); ?>"type="text">
<td><input name="another_one" style="background-color:#FFFFCC;width:60px;" maxlength="10"  value="<?php echo get_value('another_one'); ?>"type="text">

if you have a form with more than about 5 fields in it, you would want to dynamically produce it rather than typing-out/copy-pasting code 50 times. you would define the fields somewhere (array, database table) and use php code to produce the form by looping over the definition.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.