NiTx Posted March 13, 2013 Share Posted March 13, 2013 Hi, I'm trying to make a form that validates any errors on a page then submits the data via a $_SESSION to the next page for further review. I'm at the point where I'm trying to make it so that if there are any errors on the first page that form information isn't lost when the page reloads. So far this is fine for my text fields, drop down boxes etc, however I'm having issues with my check boxes. If they have been checked and submitted and the admin wants to uncheck it, when the page reloads it still comes back as being checked which means my code for unsetting the variable isnt working for whatever reason. Heres my code. <?php require_once("includes/session.php"); ?> <?php if(isset($_POST['submit'])) { $errors = array(); $required_fields = array( 'brand_id', 'dev_model', 'freq_2g_gsm850', ); //If any of the required fields are not set or empty, put them into an errors array and clear the $_SESSION variable foreach($required_fields as $fieldname) { if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) { $errors[] = $fieldname; unset($_SESSION['$fieldname']); } } //If there are no errors go ahead and process the data if (empty($errors)) { //Process each variable and add it to the Session for the next page to process *** Might be able to remove if(isset) foreach($required_fields as $fieldname) { if (isset($_POST[$fieldname])) { $_SESSION[$fieldname] = mysql_prep($_POST[$fieldname]); } } //Redirect to posttest.php to complete the device registration form redirect_to('posttest.php'); } else { foreach($required_fields as $fieldname) { if (isset($_POST[$fieldname])) { $_SESSION[$fieldname] = mysql_prep($_POST[$fieldname]); } } //unset($_SESSION['$freq_2g_gsm850']); //Errors occured $message = "There were " . count($errors) . " errors in the form"; } } ?> <h3>2G GSM</h3> <?php if(!isset($_SESSION['freq_2g_gsm850']) || empty($_SESSION['freq_2g_gsm850'])) { echo "<label><input type=\"checkbox\" name=\"freq_2g_gsm850\" value=\"850\" id=\"freq_2g_gsm850\">850</label>"; } else { echo "<label><input type=\"checkbox\" name=\"freq_2g_gsm850\" value=\"850\" id=\"freq_2g_gsm850\" checked>850</label>"; } ?> Thanks in advance for your help Quote Link to comment https://forums.phpfreaks.com/topic/275577-cant-unset-_session-variable/ Share on other sites More sharing options...
teynon Posted March 13, 2013 Share Posted March 13, 2013 unset($_SESSION['$fieldname']); Single quotes is a string literal. So you are unsetting the string "$fieldname" not the variable. unset($_SESSION[$fieldname]); Stop wrapping variables in quotes. Quote Link to comment https://forums.phpfreaks.com/topic/275577-cant-unset-_session-variable/#findComment-1418289 Share on other sites More sharing options...
NiTx Posted March 13, 2013 Author Share Posted March 13, 2013 Ahhh silly me. Thank you kind sir - that did the trick :-) Quote Link to comment https://forums.phpfreaks.com/topic/275577-cant-unset-_session-variable/#findComment-1418292 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.