garyed Posted May 19, 2013 Share Posted May 19, 2013 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 More sharing options...
garyed Posted May 19, 2013 Author Share Posted May 19, 2013 Since no one has replied can I assume that there is nothing wrong with my code & I just need to do the same thing for all my variables that I want used in the session? Link to comment https://forums.phpfreaks.com/topic/278151-help-with-session-variables-needed/#findComment-1431016 Share on other sites More sharing options...
mac_gyver Posted May 19, 2013 Share Posted May 19, 2013 you can save all the data using one statement - $_SESSION['post'] = $_POST; you would reference any specific value using - $_SESSION['post']['windows'] Link to comment https://forums.phpfreaks.com/topic/278151-help-with-session-variables-needed/#findComment-1431019 Share on other sites More sharing options...
Eiseth Posted May 19, 2013 Share Posted May 19, 2013 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"> Link to comment https://forums.phpfreaks.com/topic/278151-help-with-session-variables-needed/#findComment-1431021 Share on other sites More sharing options...
garyed Posted May 20, 2013 Author Share Posted May 20, 2013 Thanks to both of you for the help, I really like the idea of using a function too. Link to comment https://forums.phpfreaks.com/topic/278151-help-with-session-variables-needed/#findComment-1431088 Share on other sites More sharing options...
mac_gyver Posted May 20, 2013 Share Posted May 20, 2013 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 https://forums.phpfreaks.com/topic/278151-help-with-session-variables-needed/#findComment-1431098 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.