Jump to content

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/275577-cant-unset-_session-variable/
Share on other sites

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.