Reputation: 207
here is my php form code...
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page Title Goes Here</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="form1.css"/>
</head>
<body>
<?php
// define variables and set to empty value
$firstNameError = "";
$lastNameError = "";
$error = false;
// if firstName is empty, make it NULL, else, test_input() the data.
$firstName = empty($_POST["firstName"]) ? NULL : test_input($_POST["firstName"]);
// if lastName is empty, make it NULL, else, test_input() the data.
$lastName = empty($_POST["lastName"]) ? NULL : test_input($_POST["lastName"]);
if (isset($_POST["submittingForm"])) {
/// CHECK FIRST NAME ERRORS
if ($firstName === NULL) {
// firstName is empty
$firstNameError = "First name is required!";
$error = true;
} else {
// check characters
if (!preg_match("/^[a-zA-Z ]*$/", $firstName)) {
$firstNameError = "Only letters and white spaces allowed!";
$error = true;
}
}
/// CHECK LAST NAME ERRORS
if (!preg_match("/^[a-zA-Z ]*$/", $lastName)) {
// check characters
$lastNameError = "Only letters and white spaces allowed!";
$error = true;
}
// if no error then redirect
if (!$error) {
$_SESSION['fistName'] = $firstName;
$_SESSION['lastName'] = $lastName;
header('Location: testing2.php');
exit();
}
} else {
// user did not submit form!
}
// clean input
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div id="wrapper">
<h1>Welcome to Chollerton Tearoom! </h1>
<form id="userdetail" method="POST">
<fieldset id="aboutyou">
<legend id="legendauto">user information</legend>
<p>
<label for="firstName">First Name: </label>
<input type="text" name="firstName" id="firstName" value="<?php echo $firstName; ?>">
<span class="error">* <?php echo $firstNameError;?></span>
<label for="lastName">Last Name: </label>
<input type="text" name="lastName" id="lastName" value="<?php echo $lastName; ?>">
<span class="error">* <?php echo $lastNameError;?></span>
</p>
<p>
<input type="submit" name="submittingForm" value="submit">
</p>
</fieldset>
</form>
</div>
</body>
</html>
and so here is my testing2.php which get the data after submited...
<?php
session_start();
$firstName = $_SESSION['firstName'];
$lastName = $_SESSION['lastName'];
echo "<h1>Successfull submission :</h1>";
echo "<p>fitstName : $firstName <p/>";
echo "<p>lastName : $lastName <p/>";
?>
when the form is submited , Notice: Undefined index: firstName in F:\xampp\htdocs\en407b\assigment\testing2.php on line 5 is appear... i've tried to fix it but still cnt get it right... pls help me....
Upvotes: 1
Views: 37
Reputation: 136
change like this
if (!$error) {
$_SESSION['firstName'] = $firstName;//error here
$_SESSION['lastName'] = $lastName;
header('Location: testing2.php');
exit();
}
Upvotes: 1
Reputation: 8369
You have typo in your session name. You are assigning session to $_SESSION['fistName']
and trying to access it with $_SESSION['firstName'];
Upvotes: 0
Reputation: 3813
You have a simple typo -
if (!$error) {
$_SESSION['fistName'] = $firstName;
should be:
if (!$error) {
$_SESSION['firstName'] = $firstName;
I'd also recommend giving your form an action:
<form id="userdetail" action="testing2.php" method="POST">
and then move all the form processing logic over into that file. That would eliminate the need for a header redirect.
Upvotes: 0