Possible Duplicate:
Undefined Index when using post
I’m a beginner php developer and I’m just playing around with some stuff. I’m trying to create a simple registration form:
registration.php
<?php
include('Auth.php');
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email= $_POST['email'];
$password = $_POST['password'];
$auth = new Auth();
$auth->register($first_name, $last_name, $email, $password);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration</title>
</head>
<body>
<form action="registration.php" method="POST">
<p> First Name: <input type="text" size="20" name="first_name" id="first_name" value="" /> </p>
<p> Last Name: <input type="text" size="40" name="last_name" id="last_name" value="" /></p>
<p> Email: <input type="text" size="60" name="email" id="email" value="" /></p>
<p> Password: <input type="password" size="16" name="password" id="password" value="" /></p>
<input type="submit" value="submit" id="submit"/>
</form>
</body>
</html>
I get these errors:
Notice: Undefined index: first_name in C:\xampp\htdocs\Store\Store\Controller\Registration.php on line 6
Notice: Undefined index: last_name in C:\xampp\htdocs\Store\Store\Controller\Registration.php on line 7
Notice: Undefined index: email in C:\xampp\htdocs\Store\Controller\Registration.php on line 8
Notice: Undefined index: password in C:\xampp\htdocs\Store\Store\Controller\Registration.php on line 9
I’m guessing it’s because the POST variable has nothing inside it yet because nothing has been submitted. I’ve seen around on the internet where the php processing form and the html form is in the same file as opposed to two separate files like what I’m trying to do. Where the html form refers back to itself. How can I go around this? Is this even good practice when it comes to separation of content and data processing?
I
Note that these are just notices, which you should have then enabled in development environment but disabled when your site goes live. If you don’t have access to the php.in file you can do that by using this line of code:
To your problem now, it is indeed the fact that your variables are empty. You can set a name element into your submit button let’s say
<input type="submit" value="submit" name="submit" id="submit"/>and then:
That way, you will make sure that this fraction of code will execute only when your specific form is submitted.