garyed Posted May 22, 2013 Share Posted May 22, 2013 I posted a little while back & got some help with a page I'm working on with about 50 session variables. I used a function as suggested & everything works fine except when I blank out a field that had data already input into it & was posted to the page the old data returns after posted again. If I put a zero in the input field then the zero will stay after posted but if i leave the field blank whatever was last posted returns to the input field. It's been driving me crazy. Any ideas what I'm doing wrong? Here's an example of the code: <?php session_start(); function get_value($var) { if ($_POST[$var]!="") { $_SESSION[$var]=$_POST[$var];} if (isset($_SESSION[$var])){ echo $_SESSION[$var];}else{ echo $_POST[$var];} } ?> <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 get_value('windows'); ?>"type="text"> </td> </tr> <tr><td> <input type="submit" value="Calculate"></td></tr> </tbody></table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/278274-cant-get-rid-of-session-variable/ Share on other sites More sharing options...
DaveyK Posted May 22, 2013 Share Posted May 22, 2013 Delete the sessions when you are done using them? I dont really understand why you are using sessions in this form... You check if the $_POST is not empty ( You couldve used if(!empty($_POST[$var]) herre as well, but okay) and if it isnt empty, you set it to a session and you then check the session and return either echo the session of the post variable (which will always have the same value?!). Why are you using sessions for this? Also, its good practise to make the function return something, rather than echoing it. Quote Link to comment https://forums.phpfreaks.com/topic/278274-cant-get-rid-of-session-variable/#findComment-1431538 Share on other sites More sharing options...
Jessica Posted May 22, 2013 Share Posted May 22, 2013 Moving this to the right forum Quote Link to comment https://forums.phpfreaks.com/topic/278274-cant-get-rid-of-session-variable/#findComment-1431543 Share on other sites More sharing options...
mac_gyver Posted May 22, 2013 Share Posted May 22, 2013 (edited) your get_value() function should do one thing, what its name implies. it should get the value from the correct $_SESSION variable. all the values should either be in session variables or they don't exists at all at the point where you are building the form and if they do exist they should have been stored in the session variables at one point, where you detected that the form was submitted, in your form processing code. you should have specific and distinct code that process the form data and specific and distinct code that builds the form. Edited May 22, 2013 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/278274-cant-get-rid-of-session-variable/#findComment-1431549 Share on other sites More sharing options...
garyed Posted May 22, 2013 Author Share Posted May 22, 2013 Delete the sessions when you are done using them? I dont really understand why you are using sessions in this form... You check if the $_POST is not empty ( You couldve used if(!empty($_POST[$var]) herre as well, but okay) and if it isnt empty, you set it to a session and you then check the session and return either echo the session of the post variable (which will always have the same value?!). Why are you using sessions for this? Also, its good practise to make the function return something, rather than echoing it. Thanks for the help, The reason I'm using sessions is because I have about 50 inputs on the page & the biggest complaint I have is that people have to leave the page some times to change other factors on another page, return back to the page & then all their data is lost. The way I've got it now If they want to delete any of their inputs they have to change the number to "0" but just deleting the input & leaving nothing doesn't work. I'm obviously not very good at this stuff so some of what you are saying is probably over my head. I really need to see some sort of coded explanation of how to do this. Quote Link to comment https://forums.phpfreaks.com/topic/278274-cant-get-rid-of-session-variable/#findComment-1431555 Share on other sites More sharing options...
garyed Posted May 22, 2013 Author Share Posted May 22, 2013 your get_value() function should do one thing, what its name implies. it should get the value from the correct $_SESSION variable. all the values should either be in session variables or they don't exists at all at the point where you are building the form and if they do exist they should have been stored in the session variables at one point, where you detected that the form was submitted, in your form processing code. you should have specific and distinct code that process the form data and specific and distinct code that builds the form. Thanks for the reply, I've never used sessions before so I need to learn a good bit more before I'll be able to understand how to implement the things you suggested. I was hoping I was missing a simple line or phrase to solve my problem but it sounds a lot more involved. Quote Link to comment https://forums.phpfreaks.com/topic/278274-cant-get-rid-of-session-variable/#findComment-1431559 Share on other sites More sharing options...
Eiseth Posted May 22, 2013 Share Posted May 22, 2013 Because of this code if ($_POST[$var]!="") { $_SESSION[$var]=$_POST[$var];} Whenever you input nothing("") the condition will be false thus your session will not change Try something like // this will check your session if it's not empty and you send an empty post if ($_POST[$var]!="" || (!empty($_SESSION[$var]) && $_POST[$var] === '')) { $_SESSION[$var]=$_POST[$var]; } Quote Link to comment https://forums.phpfreaks.com/topic/278274-cant-get-rid-of-session-variable/#findComment-1431566 Share on other sites More sharing options...
mac_gyver Posted May 22, 2013 Share Posted May 22, 2013 you are making this harder that it is. a function should do one thing really well. a function named get_value() should just do that one thing. a function should also return the value so that you can use the function in any context. if you use the function in your form, you can echo the returned value. if you need to put the value into a query, you can call the function at the point where you are putting values into a query. Quote Link to comment https://forums.phpfreaks.com/topic/278274-cant-get-rid-of-session-variable/#findComment-1431567 Share on other sites More sharing options...
garyed Posted May 22, 2013 Author Share Posted May 22, 2013 Because of this code if ($_POST[$var]!="") { $_SESSION[$var]=$_POST[$var];} Whenever you input nothing("") the condition will be false thus your session will not change Try something like // this will check your session if it's not empty and you send an empty post if ($_POST[$var]!="" || (!empty($_SESSION[$var]) && $_POST[$var] === '')) { $_SESSION[$var]=$_POST[$var]; } That did the trick, Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/278274-cant-get-rid-of-session-variable/#findComment-1431612 Share on other sites More sharing options...
garyed Posted May 22, 2013 Author Share Posted May 22, 2013 (edited) I want to thank everyone for the help which has not only solved my problem but given me a lot of ideas to learn from. I may try some different options as a learning experience to see if i can get a better understanding. I did use a return value of the function & echoed the function instead of using echo in the function. It works either way but is that considered better technique or does it really matter? <?php session_start(); function get_value($var) { if ($_POST[$var]!="" || (!empty($_SESSION[$var]) && $_POST[$var] === '')) { $_SESSION[$var]=$_POST[$var]; } if (isset($_SESSION[$var])){ return $_SESSION[$var];}else{ return $_POST[$var];} } ?> <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 echo get_value('windows'); ?>"type="text"> </td> </tr> <tr><td> <input type="submit" value="Calculate"></td></tr> </tbody></table> </form> Edited May 22, 2013 by garyed Quote Link to comment https://forums.phpfreaks.com/topic/278274-cant-get-rid-of-session-variable/#findComment-1431625 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.