wzshop Posted October 17, 2011 Share Posted October 17, 2011 Hello, I have a simple multiple-step form and I use sessions to save the data. Now i have 1 single checkbox and i would like to know how i can save that data (if one checked it or not). I am not very advanced with sessions, nor PHP, thus i tried the following: if (isset($_POST['newsletter'])) { $_SESSION['newsletter']=$_POST['newsletter']; } $newsletter = $_SESSION['newsletter']; // do not really know what if statement to use if($newsletter == 'yes') { $newsl = '1'; } //checkbox html here <input type="checkbox" class="styled" name="newsletter" value="yes" /> Obviously the above won't work. Can explain to me how to save checkbox data in a session? Thanks a lot in advance. Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/ Share on other sites More sharing options...
kney Posted October 17, 2011 Share Posted October 17, 2011 try putting this in front of ur checkbox <form method="post" action=""> and this after </form> Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279948 Share on other sites More sharing options...
wzshop Posted October 17, 2011 Author Share Posted October 17, 2011 Hi there, Sorry forgot that in the code above, have it in my original code though.. Thanks for your reply! Robbert Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279949 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 17, 2011 Share Posted October 17, 2011 post your full form code. Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279956 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 17, 2011 Share Posted October 17, 2011 you also didn't add <?php session_start(); ?> to the start of your code. Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279957 Share on other sites More sharing options...
wzshop Posted October 17, 2011 Author Share Posted October 17, 2011 i am very sorry.. Forgot that too, have it in my original code... I actually expected that the code i "improvised" was not good at all, but that is not the case? Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279958 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 17, 2011 Share Posted October 17, 2011 so what is your form code? you should have something like this form.html <form action="check.php" method="POST"> Is this how you do it? <br/> YES:<input type="checkbox" class="styled" name="newsletter" value="yes" /> <input type="submit" value="submit"/> </form> check.php <?php session_start(); if (isset($_POST['newsletter'])) { $_SESSION['newsletter'] = $_POST['newsletter']; $newsletter = $_SESSION['newsletter']; } if($newsletter == "yes") { $newsl = 1; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279961 Share on other sites More sharing options...
wzshop Posted October 17, 2011 Author Share Posted October 17, 2011 hmm thanks a lot, i placed $newsletter = $_SESSION['newsletter']; outside the } . When i place it inside the } it works! Thanks again:) Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279962 Share on other sites More sharing options...
wzshop Posted October 17, 2011 Author Share Posted October 17, 2011 hmm although the problem is now that he does not save $newsletter to be used in a later stadium of the contactform right? When i trie to echo $newsl nothing comes up.. Is it possible to bypass that? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279963 Share on other sites More sharing options...
wzshop Posted October 17, 2011 Author Share Posted October 17, 2011 The code you gave is similar to mine. But i have another statement: <?php session_start(); if (isset($_POST['newsletter'])) { $_SESSION['newsletter'] = $_POST['newsletter']; $newsletter = $_SESSION['newsletter']; } if($newsletter == "yes") { $newsl = 1; } //later in the code i use an if statement if($newsl == "1"){ echo"example"; } ?> When i put $newsletter = $_SESSION['newsletter']; outside the }, the word "example" will be echoot no matter if i checked the checkbox or not. Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279964 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 17, 2011 Share Posted October 17, 2011 its because you already have the session store in your browser. clear them. <?php session_start(); if (isset($_POST['newsletter'])) { $_SESSION['newsletter'] = $_POST['newsletter']; } $newsletter = $_SESSION['newsletter']; if($newsletter == "yes") { $newsl = 1; echo $newsl; } if($newsl == 1){ echo "example"; } // this will destory the sessions. session_destroy(); ?> tested everything and its working even if the $newsletter = $_SESSION['newsletter']; is outside the bracket. Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279966 Share on other sites More sharing options...
wzshop Posted October 17, 2011 Author Share Posted October 17, 2011 you seem to be right Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279967 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 17, 2011 Share Posted October 17, 2011 no problem Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279968 Share on other sites More sharing options...
wzshop Posted October 17, 2011 Author Share Posted October 17, 2011 Sorry, one more question. Is there any way to (dubble) check if the checkbox is checked or not, everytime the form submits? For instance, if i check the checkbox, submit the form, go back and uncheck the checkbox.. it will stay checked in the session. Try my script at: http://www.maakmijnportret.nl/test3.php <?php session_start(); // start up your PHP session! if (isset($_POST['position'])) { $_SESSION['position']=$_POST['position']; } if (isset($_POST['name'])) { $_SESSION['name']=$_POST['name']; } if (isset($_POST['newsletter'])) { $_SESSION['newsletter']=$_POST['newsletter']; } $name = $_SESSION['name']; $position = $_SESSION['position']; $newsletter = $_SESSION['newsletter']; // eerste formulier if(!isset($_POST['overview']) && !isset($_POST['btn'])) { ?> <form action="<? $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data"> <input type="text" value="" name="position" /> <input type="checkbox" class="styled" name="newsletter" value="on" <? if($newsletter == 'on') { echo "checked";} ?> /> <input type="submit" name="btn" /> </form> <?php } // form 2 if(isset($_POST['btn'])) { ?><form action="<? $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data"> <input type="text" value="" name="name" /> <input type="submit" name="overview" /> </form> <?php } if(isset($_POST['overview'])) { echo $position; echo $name; if($newsletter == 'on') { echo "testing"; } } ?> Once the checkbox is checked, "testing" will keep showing up. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279974 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 17, 2011 Share Posted October 17, 2011 please do not use short tags. i've re-coded your script. do try it. <?php session_start(); if (isset($_POST['position']) && !empty($_POST['position'])) { $_SESSION['position'] = $_POST['position']; } if (isset($_POST['name']) && !empty($_POST['name'])) { $_SESSION['name'] = $_POST['name']; } if (isset($_POST['newsletter']) && !empty($_POST['newsletter'])) { $_SESSION['newsletter'] = $_POST['newsletter']; } $name = $_SESSION['name']; $position = $_SESSION['position']; $newsletter = $_SESSION['newsletter']; if(empty($position) && empty($name)) { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <input type="text" value="" name="position" /> <input type="checkbox" class="styled" name="newsletter" value="on" <?php if($newsletter == 'on') { echo "checked";} ?> /> <input type="submit" name="btn" /> </form> <?php } if(!empty($position) && empty($name)) { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <input type="text" value="" name="name" /> <input type="submit" name="overview" /> </form> <?php } if(!empty($name)) { echo $position; echo $name; if($newsletter == 'on') { echo "Newletter on!"; } session_destroy(); } ?> EDIT: i've just updated the new version. the above code is the new one. take note. Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279985 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 17, 2011 Share Posted October 17, 2011 just realised there was some errors in my script. the code below is tested and working. <?php session_start(); if (isset($_POST['position']) && !empty($_POST['position'])) { $_SESSION['position'] = $_POST['position']; } if (isset($_POST['name']) && !empty($_POST['name'])) { $_SESSION['name'] = $_POST['name']; } if (isset($_POST['newsletter']) && !empty($_POST['newsletter'])) { $_SESSION['newsletter'] = $_POST['newsletter']; } $name = $_SESSION['name']; $position = $_SESSION['position']; $newsletter = $_SESSION['newsletter']; if(empty($position) && empty($name)) { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <input type="text" value="" name="position" /> <input type="checkbox" class="styled" name="newsletter" value="on" <?php if($newsletter == 'on') { echo "checked";} ?> /> <input type="submit" name="btn" /> </form> <?php } if(!empty($position) && empty($name)) { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <input type="text" value="" name="name" /> <input type="submit" name="overview" /> </form> <?php } if(!empty($name)) { echo $position; echo $name; if($newsletter == 'on') { echo "Newletter on!"; } session_destroy(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279986 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 17, 2011 Share Posted October 17, 2011 did it worked? Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279991 Share on other sites More sharing options...
wzshop Posted October 17, 2011 Author Share Posted October 17, 2011 Thank you sooo much! works like a charm. I will follow up your advice Grts, Robbert Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1279999 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 17, 2011 Share Posted October 17, 2011 no prob. feel free to ask question at this forum if you need. please mark topic as solved Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1280001 Share on other sites More sharing options...
wzshop Posted October 17, 2011 Author Share Posted October 17, 2011 done, one question though; is it also possible to go back in the session. Thus let's say you're here if(!empty($position) && empty($name)) and you want 1 step back in the form, for instance to uncheck the box, is that possible to implement or? Thanks again, Robbert Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1280005 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 17, 2011 Share Posted October 17, 2011 Done. you may want to test it out it works. <?php session_start(); if (isset($_POST['position']) && !empty($_POST['position'])) { $_SESSION['position'] = $_POST['position']; } if (isset($_POST['name']) && !empty($_POST['name'])) { $_SESSION['name'] = $_POST['name']; } if (isset($_POST['newsletter']) && !empty($_POST['newsletter'])) { $_SESSION['newsletter'] = $_POST['newsletter']; } $name = $_SESSION['name']; $position = $_SESSION['position']; $newsletter = $_SESSION['newsletter']; if(isset($_GET['backto']) && !empty($_GET['backto'])) { $back2 = $_GET['backto']; if($back2 == "reset") { $position = ""; $name = ""; session_destroy(); } if($back2 == "name") { $name = ""; } if($back2 == "position") { $position = ""; $name = ""; } } if(empty($position) && empty($name)) { $name = $_SESSION['name']; $position = $_SESSION['position']; $newsletter = $_SESSION['newsletter']; ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <input type="text" value="<?php echo $position; ?>" name="position" /> <input type="checkbox" class="styled" name="newsletter" value="on" <?php if($newsletter == 'on') { echo "checked";} ?> /> <input type="submit" name="btn" /> </form> <?php session_destroy(); $name = ""; $position = ""; $newsletter = ""; } if(!empty($position) && empty($name)) { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <input type="text" value="" name="name" /> <input type="submit" name="overview" /> </form> Go <a href="<?php echo $_SERVER['PHP_SELF']; ?>?backto=position">back</a> <?php } if(!empty($name)) { echo $position; echo $name; if($newsletter == 'on') { echo "Newletter on!"; } echo 'Go <a href="'. $_SERVER['PHP_SELF'].'?backto=name">back</a>'; echo 'Go <a href="'. $_SERVER['PHP_SELF'].'?backto=reset">reset</a>'; } ?> EDIT: ive changed the script a little. Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1280009 Share on other sites More sharing options...
wzshop Posted October 17, 2011 Author Share Posted October 17, 2011 Works great! except the "reset" function at the end gives an error: Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session in /home/musicv/public_html/maakmijnportret.nl/test3.php on line 49 is that just a warning or a bug? Thanks, learn a lot;) Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1280011 Share on other sites More sharing options...
wzshop Posted October 17, 2011 Author Share Posted October 17, 2011 Sorry to bother you once more, I am trying to understand the code.. Why did you not use the if(isset($_POST['btn'])) function? Is that some outdated code or? Anyway, while i was trying to understand the code, I deleted some code to see what exact function it fulfills. I found that the code below, actually does what it should.. thus i was therefore wondering if you could please further explain your given solution.. See the script live here: http://www.maakmijnportret.nl/test3.php <?php session_start(); if (isset($_POST['position']) && !empty($_POST['position'])) { $_SESSION['position'] = $_POST['position']; } if (isset($_POST['name']) && !empty($_POST['name'])) { $_SESSION['name'] = $_POST['name']; } if (isset($_POST['newsletter']) && !empty($_POST['newsletter'])) { $_SESSION['newsletter'] = $_POST['newsletter']; } $name = $_SESSION['name']; $position = $_SESSION['position']; $newsletter = $_SESSION['newsletter']; if(!isset($_POST['btn']) && !isset($_POST['overview'])) { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <input type="text" value="<?php echo $position; ?>" name="position" /> <input type="checkbox" class="styled" name="newsletter" value="on" <?php if($newsletter == 'on') { echo "checked";} ?> /> <input type="submit" name="btn" /> </form> <?php session_destroy(); } if(isset($_POST['btn'])) { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <input type="text" value="" name="name" /> <input type="submit" name="overview" /> </form> Go <a href="<?php echo $_SERVER['PHP_SELF']; ?>?backto=position">back</a> <?php } if(isset($_POST['overview'])){ echo $position; echo $name; if($newsletter == 'on') { echo "Newletter on!"; } echo 'Go <a href="'. $_SERVER['PHP_SELF'].'">back</a>'; } ?> Thanks again, Robbert Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1280046 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 18, 2011 Share Posted October 18, 2011 if you were to submit a blank input, it would still submit. whereas my code verify if the input is blank or not. Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1280142 Share on other sites More sharing options...
wzshop Posted October 18, 2011 Author Share Posted October 18, 2011 ok thanks, thus if i implement my own custom form validation, it is all good? Quote Link to comment https://forums.phpfreaks.com/topic/249262-single-checkbox-and-_session/#findComment-1280144 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.