Form works, but my "Saved! message does not

I have created a form where are user can update the admin database on the backend. The form works great, but I can’t seem to get the “Saved” message that I created to go away when I navigate away from the page.

What I would like to happen is when you refresh the page or navigate away from the page and then go back, I want the “Saved” message to be gone. Here is the code:


<?php $title = 'Organization'; ?>

<!DOCTYPE html>

<head>
<title><?php if (isset($title)) {echo $title . ' | ';} ?>Genesis</title>

<meta name="robots" content="none" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<link href='http://fonts.googleapis.com/css?family=Adamina' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="/genesis/css/screen.css" />

</head>

<body>

<?php // Pull in fields that are currently in the database

	include ($_SERVER['DOCUMENT_ROOT'] . "/genesis/databaseconnect.php");

	if (!isset($_POST['submit'])) {
		
		$query = "SELECT setting, value FROM general";
		$result = $mysqli->query($query); 	
		while($row = $result->fetch_all()) { 
  			$organization = $row[0][1];
  			$address_1 = $row[1][1];
  			$address_2 = $row[2][1];
  			$city = $row[3][1];
  			$state_region = $row[4][1];
  			$zip_code = $row[5][1];
  			$country = $row[6][1];
  		}
  		$result->free();
		
	} else {
	
		// Create error array
		$errors = array();
		
		// Gather variables and validate
		if (isset($_POST['organization']) && $_POST['organization'] !== '' && $_POST['organization'] !== 'This field is required.') {
			$organization = trim($_POST['organization']);
		} else {
			$errors['organization'] = 'This field is required.';
			$organization = NULL;
		}
		
		if (isset($_POST['address_1']) && $_POST['address_1'] !== '' && $_POST['address_1'] !== 'This field is required.') {
			$address_1 = trim($_POST['address_1']);
		} else {
			$errors['address_1'] = 'This field is required.';
			$address_1 = NULL;
		}
		
		if (isset($_POST['address_2']) && $_POST['address_2'] !== '') {
			$address_2 = trim($_POST['address_2']);
		} else {
			$address_2 = NULL;
		}
		
		if (isset($_POST['city']) && $_POST['city'] !== ''&& $_POST['city'] !== 'This field is required.') {
			$city = trim($_POST['city']);
		} else {
			$errors['city'] = 'This field is required.';
			$city = NULL;
		}
		
		if (isset($_POST['state_region']) && $_POST['state_region'] !== ''&& $_POST['state_region'] !== 'This field is required.') {
			$state_region = trim($_POST['state_region']);
		} else {
			$errors['state_region'] = 'This field is required.';
			$state_region = NULL;
		}
		
		if (isset($_POST['zip_code']) && $_POST['zip_code'] !== ''&& $_POST['zip_code'] !== 'This field is required.') {
			$zip_code = trim($_POST['zip_code']);
		} else {
			$errors['zip_code'] = 'This field is required.';
			$zip_code = NULL;
		}
		
		if (isset($_POST['country']) && $_POST['country'] !== ''&& $_POST['country'] !== 'This field is required.') {
			$country = trim($_POST['country']);
		} else {
			$errors['country'] = 'This field is required.';
			$country = NULL;
		}
		
	}

?>

<div class="wrap">
	<div class="header">
		<div class="logo">
			<h1><a href="/genesis/admin/index.php">St. William the Abbot</a></h1>
			<address>2000 Jackson Ave., Seaford, NY 11783</address>
		</div> <!-- END: .logo -->
		<div class="breadcrumbs">
			<ul>
				<li><a href="/genesis/admin/general/organization.php">Organization</a></li>
				<li class="last"><a href="/genesis/admin/index.php">The Board</a></li>
			</ul>
		</div> <!-- END: .breadcrumbs -->
	</div> <!-- END: .header -->
	<?php include ($_SERVER['DOCUMENT_ROOT'] . "/genesis/includes/nav.php"); ?>
	<div class="content">
		<h2>Organization</h2>
		<h3>General Information</h3>
		<?php 
			// If error array is empty
			if (empty($errors)) {
		
				// Insert variables into database
				$query = "UPDATE general SET value='" . $organization . "' WHERE setting='organization';";
				$query .= "UPDATE general SET value='" . $address_1 . "' WHERE setting='address_1';";
				$query .= "UPDATE general SET value='" . $address_2 . "' WHERE setting='address_2';";
				$query .= "UPDATE general SET value='" . $city . "' WHERE setting='city';";
				$query .= "UPDATE general SET value='" . $state_region . "' WHERE setting='state_region';";
				$query .= "UPDATE general SET value='" . $zip_code . "' WHERE setting='zip_code';";
				$query .= "UPDATE general SET value='" . $country . "' WHERE setting='country'";
				$result = $mysqli->multi_query($query);

				echo '<div class="saved">Saved!</div>';
						
			} else {

				echo '<div class="errors">Please correct the errors below.</div>';
			
			}
		
			$mysqli->close();
		?>
		
		<form method="post" action="organization.php">
			<div><label for="organization" class="required">Organization Name <em>*</em></label><input<?php if (isset($errors['organization'])) {echo ' class="errors"';} ?> type="text" name="organization" id="organization"<?php if (isset($organization) && $organization !== '') {echo ' value="' . $organization . '"';} else if (isset($errors['organization'])) {echo 'value="' . $errors['organization'] . '"';} ?> /></div>
			<div><label for="address_1" class="required">Address 1 <em>*</em></label><input<?php if (isset($errors['address_1'])) {echo ' class="errors"';} ?> type="text" name="address_1" id="address_1"<?php if (isset($address_1) && $address_1 !== '') {echo ' value="' . $address_1 . '"';} else if (isset($errors['address_1'])) {echo 'value="' . $errors['address_1'] . '"';} ?> /></div>
			<div><label for="address_2">Address 2</label><input type="text" name="address_2" id="address_2"<?php if (isset($address_2) && $address_2 !== '') {echo ' value="' . $address_2 . '"';} ?> /></div>
			<div><label for="city" class="required">City <em>*</em></label><input<?php if (isset($errors['city'])) {echo ' class="errors"';} ?> type="text" name="city" id="city"<?php if (isset($city) && $city !== '') {echo ' value="' . $city . '"';} else if (isset($errors['city'])) {echo 'value="' . $errors['city'] . '"';} ?> /></div>
			<div><label for="state_region" class="required">State/Region <em>*</em></label><input<?php if (isset($errors['state_region'])) {echo ' class="errors"';} ?> type="text" name="state_region" id="state_region"<?php if (isset($state_region) && $state_region !== '') {echo ' value="' . $state_region . '"';} else if (isset($errors['state_region'])) {echo 'value="' . $errors['state_region'] . '"';} ?> /></div>
			<div><label for="zip_code" class="required">Zip Code <em>*</em></label><input<?php if (isset($errors['zip_code'])) {echo ' class="errors"';} ?> type="text" name="zip_code" id="zip_code"<?php if (isset($zip_code) && $zip_code !== '') {echo ' value="' . $zip_code . '"';} else if (isset($errors['zip_code'])) {echo 'value="' . $errors['zip_code'] . '"';} ?> /></div>
			<div><label for="country" class="required">Country <em>*</em></label><input<?php if (isset($errors['country'])) {echo ' class="errors"';} ?> type="text" name="country" id="country"<?php if (isset($country) && $country !== '') {echo ' value="' . $country . '"';} else if (isset($errors['country'])) {echo 'value="' . $errors['country'] . '"';} ?> /></div>
			<div class="button"><input type="Submit" name="submit" value="Update" /> <small class="required">All fields with (<em>*</em>) required.</small></div>
		</form>
	<?php
	
		unset ($organization);
		unset ($address_1);
		unset ($address_2);
		unset ($city);
		unset ($state_region);
		unset ($zip_code);
		unset ($country);
		unset ($errors);
		$result->free();
		
	?>
	</div> <!-- END: .content -->
	<div class="footer">
		<p>Copyright &copy; <?php if (date('Y') == '2011') {echo '2011';} else {echo '2011-' . date('Y');} ?>, <a href="#">Genesis</a>.  All rights reserved.<br />A company of <a href="http://www.jameswebdevelopment.com">James Web Development</a>.</p>
	</div> <!-- END: .footer -->
</div> <!-- END: .wrap -->

</body>

</html>

You will find the “Saved” box in the Content area. I’m not sure if I put it in the right place. Tried a few different places, but still had the problem.

Any help would be appreciated!

I… dont understand your form flow, really, but lets give this a shot.
Move $errors = array(); to right after your db include. Now $errors always exists and wont throw an error when you try and check if it’s empty later on.

            if (empty($errors)) { 

=>

            if (empty($errors) && isset($_POST['submit']) { 

BTW, you know you can do all those UPDATE’s in one query, right?

But the reason that I have the errors array where I have it is so that the position of the message will be right above the form (see the attached screenshot). If I move the errors array where you told me to, the whole form disappeared.

I’m a newbie to mysqli, so if you have any suggestions on how to do a multi query, I am all ears. I tried to figure it out, but I kept getting error messages. That was going to be my next forum question.

Actually i misread the queries… interesting system of configuration there.

As far as the errors go, i didnt say move the output, i said put $errors = array(); up above your if/else.

If the isset($_POST[‘submit’]) is not true, then the script never gets to the line that reads $errors = array();, so it doesnt exist when the script gets to empty($errors) - which kicks out a warning about an undefined variable. (If your server isnt set to display errors, then you wont see it, but it still kicks it). It’s not a critical thing, though.

EDIT: Actually, i just double checked, and empty DOESNT throw a warning on not set, so you’re fine.

Thanks so much for your help!

I ended up having to change the $errors if statement to…


if (empty($errors) && isset($_POST['submit'])) {
		
				// Insert variables into database
				$query = "UPDATE general SET value='" . $organization . "' WHERE setting='organization';";
				$query .= "UPDATE general SET value='" . $address_1 . "' WHERE setting='address_1';";
				$query .= "UPDATE general SET value='" . $address_2 . "' WHERE setting='address_2';";
				$query .= "UPDATE general SET value='" . $city . "' WHERE setting='city';";
				$query .= "UPDATE general SET value='" . $state_region . "' WHERE setting='state_region';";
				$query .= "UPDATE general SET value='" . $zip_code . "' WHERE setting='zip_code';";
				$query .= "UPDATE general SET value='" . $country . "' WHERE setting='country'";
				$result = $mysqli->multi_query($query);

				echo '<div class="saved">Saved!</div>';
						
			} else if (!empty($errors) && isset($_POST['submit'])) {

				echo '<div class="errors">Please correct the errors below.</div>';
			
			}

That seemed to make it work great.

Now I just have to figure out how to do the multi query.

You can clean the code up a bit by running it through a while/foreach loop, but you cannot do those queries in one query (multi_query will be the best command to use once the string is constructed). Update works on a single value/calculation.

IE:
I can tell MySQL to update a single user. “UPDATE user SET name = ‘John’ WHERE id = 2”
I can tell MySQL to update several users, with a single value.“UPDATE user SET name = ‘John’ WHERE lastname = ‘Smith’”
I can tell MySQL to update all users, with a calculated value “UPDATE user SET visits = visits + 1”
but i cannot tell mysql to update user X with value Y, and user S with value R, in a single query.