Jump to content

Undefined Error


TomTees

Recommended Posts

You are referencing some sort of an array variable using an associative index name of firstname and it does not exist.

 

You would need to determine why it does not exist and if it is normal that your code is executed when it might not exist, you should use the isset() function to test if the variable exists before referencing it.

Link to comment
Share on other sites

If you post the code where you get this error then we can assist you easier :)

 

Regards, Paul.

 

Oh, okay.

 

Here you go...

 

index.php

<?php
$FName = $_POST["firstname"];
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>User Registration Form</title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8">
	<link rel="stylesheet" type="text/css" href="registration.css">
</head>
<body>
	<h1>User Registration Form</h1>
	<form method="post" action="registration.php">
		<!-- Registration Fields -->
		<div>
			<label for="email">E-mail:</label>
			<input type="text" name="email" class="txt" id="email" />
		</div>
		<div>
			<label for="email2">Re-enter E-mail:</label>
			<input type="text" name="email2" class="txt" id="email2" />
		</div>
		<div>
			<label for="password1">Password:</label>
			<input type="password" name="password1" class="txt" id="password1" />
		</div>
		<div>
			<label for="password2">Re-enter Password:</label>
			<input type="password" name="password2" class="txt" id="password2" />
		</div>
		<div>
			<label for="firstname">First Name:</label>
			<input type="text" name="firstname" class="txt" id="firstname" />
		</div>
		<div>
			<label for="lastname">Last Name:</label>
			<input type="text" name="lastname" class="txt" id="lastname" />
		</div>
		<!-- Submit button -->
		<div>
			<input type="submit" name="btnSubmit" value="Register" class="btn" id="btnSubmit" />
		</div>
	</form>
</body>
</html>

 

 

registration.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TITLE</title>
<link type="text/css" rel="stylesheet" href="PathToStyleSheet.css">
    </head>
    <body>
		<p>
		<?php
			echo "First Name: " . $FName;
		?>
		</p>
    </body>
</html>

 

 

 

TomTees

 

 

Link to comment
Share on other sites

In the file index.php you have...

<?php
   $FName = $_POST["firstname"];
?>

This is never set as the form is posting to the registration.php, so you will always get an error.

 

Try adding the following to registration.php after the <body> tag...

<?PHP
  if($_POST['btnSubmit']) {
    $FName = $_POST["firstname"]; 
  }
?>

 

When the form is submitted providing the firstname field is not empty, it should echo out the value that was inserted into the field.

 

Hope this helps, Paul.

Link to comment
Share on other sites

In the file index.php you have...

<?php
   $FName = $_POST["firstname"];
?>

This is never set as the form is posting to the registration.php, so you will always get an error.

 

Why does it cause an error?

 

 

And how is data passed from index.php to registration.php??

 

 

Try adding the following to registration.php after the <body> tag...

<?PHP
  if($_POST['btnSubmit']) {
    $FName = $_POST["firstname"]; 
  }
?>

 

Why can't you just print $_POST["firstname"] straight up?

 

 

TomTees

 

 

Link to comment
Share on other sites

Why does it cause an error?

It causes an error because the $_POST['firstname'] does not exist unless a form is actually POST-ed.

So if the $_POST['firstname'] then the variable $FName is not set, so it will throw an error.

 

 

And how is data passed from index.php to registration.php??

In your case the data is passed using the form you have created in index.php

When a visitor or member or whatever posts that form, the data from the form will be passed to the registration.php file for you to use how you wish.

 

 

Why can't you just print $_POST["firstname"] straight up?

You can just print $_POST['firstname'] if you wish, but when error checking using form data, I always tend to put the posted data into a variable as it makes the code look much neater.

 

NOTE - Use single quotes (') over double quotes (") as PHP will process single quotes faster than double quotes.

 

Regards, Paul.

Link to comment
Share on other sites

Why does it cause an error?

It causes an error because the $_POST['firstname'] does not exist unless a form is actually POST-ed.

So if the $_POST['firstname'] then the variable $FName is not set, so it will throw an error.

 

I thought you didn't have to define variables in PHP before you used them?

 

And how is data passed from index.php to registration.php??

In your case the data is passed using the form you have created in index.php

When a visitor or member or whatever posts that form, the data from the form will be passed to the registration.php file for you to use how you wish.

 

But I mean is it how is the data in an HTML form physically transferred to another php page?

 

 

TomTees

 

 

Link to comment
Share on other sites

I thought you didn't have to define variables in PHP before you used them?

 

But you are using it. You're attempting to assign its value to another variable, and it's not yet defined.

 

But I mean is it how is the data in an HTML form physically transferred to another php page?

 

With the form's method defined as 'post', it's done via a HTTP POST request. This link is a decent explanation of how it works.

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.