TomTees Posted September 19, 2010 Share Posted September 19, 2010 What causes this error?? Notice: Undefined index: firstname in /Users/user1/Documents/DEV/htdocs/index.php on line 2 TomTees Quote Link to comment https://forums.phpfreaks.com/topic/213771-undefined-error/ Share on other sites More sharing options...
PaulRyan Posted September 19, 2010 Share Posted September 19, 2010 If you post the code where you get this error then we can assist you easier Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/213771-undefined-error/#findComment-1112638 Share on other sites More sharing options...
PFMaBiSmAd Posted September 19, 2010 Share Posted September 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/213771-undefined-error/#findComment-1112643 Share on other sites More sharing options...
TomTees Posted September 19, 2010 Author Share Posted September 19, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/213771-undefined-error/#findComment-1112647 Share on other sites More sharing options...
PaulRyan Posted September 19, 2010 Share Posted September 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/213771-undefined-error/#findComment-1112655 Share on other sites More sharing options...
TomTees Posted September 19, 2010 Author Share Posted September 19, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/213771-undefined-error/#findComment-1112662 Share on other sites More sharing options...
Mod-Jay Posted September 19, 2010 Share Posted September 19, 2010 Separate php page, unless you have it defined on a global page it wont show. otherwise you need to post it on each page you use it on Quote Link to comment https://forums.phpfreaks.com/topic/213771-undefined-error/#findComment-1112665 Share on other sites More sharing options...
PaulRyan Posted September 19, 2010 Share Posted September 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/213771-undefined-error/#findComment-1112668 Share on other sites More sharing options...
TomTees Posted September 19, 2010 Author Share Posted September 19, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/213771-undefined-error/#findComment-1112671 Share on other sites More sharing options...
Pikachu2000 Posted September 19, 2010 Share Posted September 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/213771-undefined-error/#findComment-1112676 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.