I am a novice programmer in php, just starting recently I encounntered some
problems when I load my code into the browser.
I tried to develop a form that is to be processed on thesame page.
For example , writing a form with its validation codes on the same page , I get an
Error message like this: “undefined index: form-field-Name ”.I get a message telling me that the form fields are undefined.
Initially, I would send the form field names to a second page, using the super global arrays, and then write codes to process the form on the second page.This gave no problems.
But as proceeded in my study, I came to learn how I could process the form
on thesame page intsead of sending the fields to another page to be processed
Below is one of the forms I tried to run in a browser and the errors messages I got.
Notice: Undefined index: firstName in D:\Sites\study\Practice\P2\test11.php on line 2
Notice: Undefined index: lastName in D:\Sites\study\Practice\P2\test11.php on line 3
, you have been registered
Below is the code for the form shown above.
<?php
$Fname=$POST['firstName'];
$Lname=$POST['lastName'];
echo "$Fname $Lname, you have been registered";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="POST">
<input type="text" name="firstName">
<input type="text" name="lastName">
<input type="submit" value="submit"></form>
</body>
</html>
The code below is from the book I am using to study, and in the book, these codes gave an output without error.
<?php
if ($POST['submit'] == "Submit Data")
{
echo "Your First Name is: " . $POST["FName"] . "<br/>";
echo "Your Last Name is: " . $POST["LName"] . "<br/>";
echo "Your City is: " . $POST["City"] . "<br/>";
echo "Your State is: " . $POST["State"] . "<br/>";
echo "<br/>";
echo "Your Message is: " . $POST["Message"];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD/XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml11-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>A Web Page</title>
</head>
<body>
<form action="form_process.php" method="post">
First Name: <input type="text" name="FName"/>
Last Name: <input type="text" name="LName"/>
City: <input type="text" name="City"/>
State: <input type="text" name="State"/>
Message: <textarea name="Message" cols="30" rows="5"></textarea>
<input type="submit" name="submit" value="Submit Data"/>
</form>
</body>
</html>
Please help me rectify the problem, I have no idea of what the problem is, I am using php5 and apache2
Almost all the books I am using have given examples in this way, I don’t know why exactly I am getting this error. Thankyou.