Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/278274-cant-get-rid-of-session-variable/
Share on other sites

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.

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 by mac_gyver

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.   

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.

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];
}

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.

 

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

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 by garyed
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.