Jump to content

Need help initializing Array


doubledee

Recommended Posts

I am getting an "undefined" error at these two lines...

 

<?php echo $errors['firstName']; ?>
<?php echo $errors['lastName']; ?>

 

 

Here is my file...

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title></title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link type="text/css" rel="stylesheet" href=".css">
	<style type="text/css" >
		form{
			width: 400px;
			margin: 0 auto;
		}
	</style>
</head>
<body>
	<?php
		// Initialize.
		$errors = array();
		$firstName = $lastName = '';

		if (isset($_POST['submitted'])){
			// Handle Form.

			// Trim all incoming data.
			$trimmed = array_map('trim', $_POST);

			// Check First Name.
			if (preg_match('/^[A-Z\'.-]{2,20}$/i', $_POST['firstName'])){
				$firstName = $_POST['firstNamae'];
			}else{
				$errors['firstName'] = 'Please enter your First Name.';
			}

			// Check Last Name.
			if (preg_match('/^[A-Z\'.-]{2,20}$/i', $_POST['lastName'])){
				$lastName = $_POST['lastName'];
			}else{
				$errors['lastName'] = 'Please enter your Last Name.';
			}

			// if there are errors then go back to the form and display them
		}else{

		}
	?>
	<form action="">
		<fieldset>
			<legend>Billing Details</legend>
			<ol>
				<li>
					<label for="firstName">First Name:</label>
					<input id="firstName" name="firstName" class="text" type="text" />
					<?php echo $errors['firstName']; ?>
				</li>
				<li>
					<label for="lastName">Last Name:</label>
					<input id="lastName" name="lastName" class="text" type="text" />
					<?php echo $errors['lastName']; ?>
				</li>
			</ol>
			<input class="submit" type="submit" value="Process Order" />
		</fieldset>
	</form>
</body>
</html>

 

I thought things were initialized okay?!

 

Sincerely,

 

 

Debbie

 

Link to comment
Share on other sites

You only assign a value to those array elements when $_POST['submitted'] is set.  On your first load of the form those elements don't exists.

 

But I initialized my array outside of the the If-then-Else?!

 

	<?php
		// Initialize.
		$errors = array();
		$firstName = $lastName = '';

		if (isset($_POST['submitted'])){
			// Handle Form.

 

 

 

Debbie

 

Link to comment
Share on other sites

Yes, so you have an empty array called $errors but there is no $errors['firstName'] or $errors['lastName'].  That's why it's complaining.

 

I have always found it a real pain in the ____ that on one hand PHP doesn't want you defining datatypes and initializing variables and then it complains about stuff like this?!

 

So is there an easy way to initialize my array and make the errors go away?

 

(On my actual form there could 30+ fields.  I cannot believe I have to initialize each variable key value?!)

 

 

 

Debbie

 

Link to comment
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.