Jump to content

Help with session variables needed


garyed
Go to solution Solved by 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
Share on other sites

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">
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.