I am trying to display form information with Shipping Information Heading, with validation of name, contact etc. I am able to display it but not able to validate it and if I submit a form blank it shows blank values except payment and shipping method are displaying with default values selected and one more thing it is not showing errors i.e; fname is required, I am confused a little bit. Can anyone add one things that if anyone post blank form it redirect it to again to form2.php page?
This is my form2.php page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="welcome2.php" method="post">
<label>First name: </label>
<input type="text" name="fname"><br>
<label>Lastname name: </label>
<input type="text" name="lname"><br>
<label>Email: </label>
<input type="email" name="email"><br>
<label>Contact No </label>
<input type="text" name="cno"><br>
<label>Address </label>
<input type="text" name="addr"><br>
<label>City </label>
<input type="text" name="city"><br>
<label>State </label>
<input type="text" name="state"><br>
<label>Country </label>
<input type="text" name="country"><br>
<label>Zip Code </label>
<input type="text" name="zipcode"><br>
<label>Credit Card Number </label>
<input type="text" name="ccno"><br>
<label>Payment Option </label>
<select name="Payment_option">
<option value="Cash On Delivery">Cash On Delivery</option>
<option value="Online">Online</option>
</select> <br>
<label>Shipping Method </label>
<select name="Shipping_Method">
<option value="TCS">TCS</option>
<option value="Leapord">Leapord</option>
<option value="FEDEX">FEDEX</option>
</select> <br>
<button type="submit" name="sub">Submit</button>
</form>
<?php
if(isset($_SESSION['errors']))
{
foreach($_SESSION['errors'] as $key => $error)
{
echo $error."<br>";
}
unset($_SESSION['errors']);
}
?>
</body>
</html>
and this is my welcome2.php page
<?php session_start();
$fname = "";
$lname = "";
$email = "";
$cno = "";
$addr = "";
$city = "";
$state = "";
$country = "";
$zipcode = "";
$ccno = "";
extract($_POST);
$errors = array();
if(isset($_POST['fname'])){
$_SESSION['fname'] = $_POST['fname'];
}if(isset($_POST['lname'])){
$_SESSION['lname'] = $_POST['lname'];
}if(isset($_POST['email'])){
$_SESSION['email'] = $_POST['email'];
}if(isset($_POST['cno'])){
$_SESSION['cno'] = $_POST['cno'];
}if(isset($_POST['addr'])){
$_SESSION['addr'] = $_POST['addr'];
}if(isset($_POST['city'])){
$_SESSION['city'] = $_POST['city'];
}if(isset($_POST['state'])){
$_SESSION['state'] = $_POST['state'];
}if(isset($_POST['country'])){
$_SESSION['country'] = $_POST['country'];
}if(isset($_POST['zipcode'])){
$_SESSION['zipcode'] = $_POST['zipcode'];
}if(isset($_POST['ccno'])){
$_SESSION['ccno'] = $_POST['ccno'];
}
if(isset($_POST['sub']))
{
if(!$fname)
$errors[] = "First name is required";
}
if(!$lname)
{
$errors[] = "Last name is required";
}
if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
$errors = "Only letters and white space allowed";
}
if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
$errors = "Only letters and white space allowed";
}
if(!$email)
{
$errors[] = "Email is required";
}
if(!$cno)
{
$errors[] = "Contact is required";
}if (strlen($cno)<=5)
{
$errors[] ="Contact contain more than 11 characters";
}
if(!$addr)
{
$errors[] = "Address is required";
}
if(!$city)
{
$errors[] = "City is required";
}
if(!$state)
{
$errors[] = "State is required";
}
if(!$country)
{
$errors[] = "Country is required";
}
if(!$zipcode)
{
$errors[] = "Zip Code is required";
}
if(!$ccno)
{
$errors[] = "Credit Card Number is required";
}?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1> Shipping Information </h1>
<?php echo $fname; echo "<br>";
echo $lname; echo "<br>";
echo $email; echo "<br>";
echo $cno; echo "<br>";
echo $addr; echo "<br>";
echo $city; echo "<br>";
echo $state; echo "<br>";
echo $country; echo "<br>";
echo $zipcode; echo "<br>";
echo $ccno; echo "<br>";
$option1 = isset($_POST['Payment_option']) ? $_POST['Payment_option'] : false;
if ($option1) {
echo htmlentities($_POST['Payment_option'], ENT_QUOTES, "UTF-8");
} else {
echo "Payment Method is required";
} echo "<br>";
$option2 = isset($_POST['Shipping_Method']) ? $_POST['Shipping_Method'] : false;
if ($option2) {
echo htmlentities($_POST['Shipping_Method'], ENT_QUOTES, "UTF-8");
} else {
echo "Shipping Method is required";
}
?>
You set the values at the top of the PHP file, so they are always set. Then the errors never trigger because you do not check if the $_POST[<value>] is set and then assign it to the variable.
Also, try to use loops. It helps with code maintenance and readability. Something like this could work for you:
$values = [
'fname' => 'First Name',
'lname' => 'Last Name',
'email' => 'Email',
'test' => 'testt',
];
$results = array();
$errors = array();
foreach ( $values as $name => $displayName ) {
if ( isset( $_POST[ $name ] ) )
{
$results[ $name ] = $_POST[ $name ];
}
else
{
$errors[] = $displayName . ' is required';
}
}
Your errors are being added with a faulty check. Use empty($variable) instead of the NOT operator (!).
Also, be careful how you are changing the value of your original empty variables.. I would use extract( $_POST, EXTR_OVERWRITE, 'form_' ); to be even safer and then reference by $form_fname, etc. Then use the empty() check.
I also recommend using a debugger that can step through each line with you. It works wonders.
Related
I'm new to PHP and I just want to make some form like a basic form. But I have trouble in the values entered by the user. For example if they enter a empty field it should not redirect or if in the email they didn't enter a correct format the form should not redirect. But in my case it always redirect even though the input is invalid. Can you help me out on how can I stop redirecting to another page if the value entered by the user is invalid?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FG4</title>
</head>
<style>
.error {color: red}
</style>
<body>
<?php
$fname = $lname = $fgender = $mail = $dob = $address = "";
$fnameErr = $lnameErr = $genderErr = $mailErr = $dobErr = $addressErr = "";
if ($_SERVER["REQUEST_METHOD"] == "GET") {
if (empty($_GET["fname"])) {
$fnameErr = "Please enter your first name.";
} else {
$fname = input($_GET["fname"]);
// check if name only contains letters and space
if (!preg_match("/^[a-zA-Z-' ]*$/",$fname)) {
$fnameErr = "Please enter a valid name";
}
}
if (empty($_GET["lname"])) {
$lnameErr = "Please enter your last name.";
} else {
$lname = input($_GET["lname"]);
// check if name only contains letters and space
if (!preg_match("/^[a-zA-Z-' ]*$/",$lname)) {
$lnameErr = "Please enter a valid name";
}
}
if (empty($_GET["gender"])) {
$genderErr = "Please select a gender.";
} else{
$gender = input($_GET["gender"]);
}
if (empty($_GET["mail"])) {
$mailErr = "Please enter your email.";
} else {
$mail = input($_GET["mail"]);
// check if email contain gmail.com or yahoo.com
if (!preg_match("/#gmail.com|#yahoo.com/", $mail)) {
$mailErr = "Please enter a valid email.";
}
}
if (empty($_GET["dob"])) {
$dobErr = "Please select your date of birth.";
} else{
$lname = input($_GET["lname"]);
}
if (empty($_GET["address"])) {
$addressErr = "Please enter your address.";
} else {
$address = input($_GET["address"]);
// check if address contain the following characters
if (!preg_match(" /#|[0-9]|[a-z]|[A-Z]/ ",$address)) {
$address = "Please enter a valid address";
}
}
}
function input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="get" action="trial.php">
First Name: <input type="text" name="fname">
<span class="error">* <?php echo $fnameErr;?></span>
<br><br>
Larst Name: <input type="text" name="lname">
<span class="error">* <?php echo $lnameErr;?></span>
<br><br>
Gender: <input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
Email: <input type="text" name="mail">
<span class="error">* <?php echo $mailErr;?></span>
<br><br>
Date of Birth: <input type="date" name="dob">
<span class="error">* <?php echo $dobErr;?></span>
<br><br>
Address: <br><textarea type="text" name="address" rows="5" cols="40"></textarea>
<span class="error"><?php echo $addressErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
Here is the other code where it just print the values entered by the user
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET") {
// collect value of input field
$fname = $_GET['fname'];
$lname = $_GET['lname'];
$gender = $_GET['gender'];
$mail = $_GET['mail'];
$dob = $_GET['dob'];
$address = $_GET['address'];
echo "<h2> Final Output:</h2>";
echo "First Name :$fname";
echo "<br>";
echo "Last Name :$lname";
echo "<br>";
echo "Gender :$gender";
echo "<br>";
echo "Email :$mail";
echo "<br>";
echo "Date of Birth :$dob";
echo "<br>";
echo "Address :$address";
}
?>
</body>
</html>
There are many ways to do what you want.
One of them is to use a hidden form and submit it only if there is no error found after the validation.
Hence the amended code (based on your original code) will be:
trial.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FG4</title>
</head>
<style>
.error {color: red}
</style>
<body>
<?php
$fname = $lname = $fgender = $mail = $dob = $address = "";
$fnameErr = $lnameErr = $genderErr = $mailErr = $dobErr = $addressErr = "";
if ($_SERVER["REQUEST_METHOD"] == "GET") {
if (empty($_GET["fname"])) {
$fnameErr = "Please enter your first name.";
} else {
$fname = input($_GET["fname"]);
// check if name only contains letters and space
if (!preg_match("/^[a-zA-Z-' ]*$/",$fname)) {
$fnameErr = "Please enter a valid name";
}
}
if (empty($_GET["lname"])) {
$lnameErr = "Please enter your last name.";
} else {
$lname = input($_GET["lname"]);
// check if name only contains letters and space
if (!preg_match("/^[a-zA-Z-' ]*$/",$lname)) {
$lnameErr = "Please enter a valid name";
}
}
if (empty($_GET["gender"])) {
$genderErr = "Please select a gender.";
} else{
$gender = input($_GET["gender"]);
}
if (empty($_GET["mail"])) {
$mailErr = "Please enter your email.";
} else {
$mail = input($_GET["mail"]);
// check if email contain gmail.com or yahoo.com
if (!preg_match("/#gmail.com|#yahoo.com/", $mail)) {
$mailErr = "Please enter a valid email (only #gmail.com or #yahoo.com).";
}
}
if (empty($_GET["dob"])) {
$dobErr = "Please select your date of birth.";
} else{
$lname = input($_GET["lname"]);
}
if (empty($_GET["address"])) {
$addressErr = "Please enter your address.";
} else {
$address = input($_GET["address"]);
// check if address contain the following characters
if (!preg_match(" /#|[0-9]|[a-z]|[A-Z]/ ",$address)) {
$address = "Please enter a valid address";
}
}
}
function input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form id="form_id" method="get" action=#>
First Name: <input type="text" name="fname" value="<?php echo $_GET["fname"];?>">
<span class="error">* <?php echo $fnameErr;?></span>
<br><br>
Larst Name: <input type="text" name="lname" value="<?php echo $_GET["lname"];?>">
<span class="error">* <?php echo $lnameErr;?></span>
<br><br>
Gender: <input type="radio" name="gender" value="male"
<?php if ($_GET["gender"]=="male") { echo " checked ";} ?>
> Male
<input type="radio" name="gender" value="female"
<?php if ($_GET["gender"]=="female") { echo " checked ";} ?>
> Female
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
Email: <input type="text" name="mail" value="<?php echo $_GET["mail"];?>">
<span class="error">* <?php echo $mailErr;?></span>
<br><br>
Date of Birth: <input type="date" name="dob" value="<?php echo $_GET["dob"];?>">
<span class="error">* <?php echo $dobErr;?></span>
<br><br>
Address: <br><textarea type="text" name="address" rows="5" cols="40"><?php echo $_GET["address"];?></textarea>
<span class="error"><?php echo $addressErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if ($fnameErr=="" && $lnameErr=="" && $genderErr=="" && $mailErr=="" && $dobErr=="" && $addressErr=="") { ?>
<form id="form_id2" method=GET action="trial2.php">
<input type=hidden name="fname" value="<?php echo $_GET["fname"];?>">
<input type=hidden name="lname" value="<?php echo $_GET["lname"];?>">
<input type=hidden name="gender" value="<?php echo $_GET["gender"];?>">
<input type=hidden name="mail" value="<?php echo $_GET["mail"];?>">
<input type=hidden name="dob" value="<?php echo $_GET["dob"];?>">
<textarea name=address style="display:none;"><?php echo $_GET["address"];?></textarea>
</form>
<script>
document.getElementById("form_id2").submit();
</script>
<?php } ?>
trial2.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET") {
// collect value of input field
$fname = $_GET['fname'];
$lname = $_GET['lname'];
$gender = $_GET['gender'];
$mail = $_GET['mail'];
$dob = $_GET['dob'];
$address = $_GET['address'];
echo "<h2> Final Output:</h2>";
echo "First Name :$fname";
echo "<br>";
echo "Last Name :$lname";
echo "<br>";
echo "Gender :$gender";
echo "<br>";
echo "Email :$mail";
echo "<br>";
echo "Date of Birth :$dob";
echo "<br>";
echo "Address :$address";
}
?>
</body>
</html>
I currently have my code working to some state.
When the user inputs data name, email and company they submit the form and it will echo the inputs out which is fine, but when I enter invalid data into the form and submit it will still post but displays the else statement.
Have I missed something in my Preg_match or is this just a bad way to code the validation?
<!DOCTYPE html>
<html>
<head>
<title>Visitor Sign in</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="visitor.css"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<div id="wrapper">
<img src="Wincanton.png" alt="wincantonLogo" class="wincantonLogo" />
<img src="Screwfix.png" alt="screwfixLogo" class="screwfixLogo" />
<div style="clear:both"></div><br>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $companyErr = "";
$fullname = $email = $company = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["fullname"])) {
$nameErr = "Name is required";
} else {
$fullname = test_input($_POST["fullname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$fullname)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!preg_match("/^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/",$email)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["company"])) {
$companyErr = "Name is required";
} else {
$company = test_input($_POST["company"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$company)) {
$companyErr = "Only letters and white space allowed";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h1>Visitor Sign in</h1><br>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="fullname" >
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Company: <input type="text" name="company">
<span class="error"><?php echo $companyErr;?></span>
<br><br>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $fullname;
echo "<br>";
echo $email;
echo "<br>";
echo $company;
echo "<br>";
?>
</body>
</html>
try if isset condition.
if(isset($_POST['submit'])){
}
I created the fields that has validation process like required fields, numbers only and valid email.
it displays the errors simultaneously after submit but upon changing only one of the fields, it accepts and does not revalidate the other.
example
name = Error : required field
telephone = Error : numbers only
email = Error : not a valid email
after i corrected only the email , it accepts and proceed on submitting without rechecking the others.
please see my code . thanks in advance
<?php
include("conn/db.php");
function renderForm($name ='', $tel = '', $email ='', $error='', $error2='', $error3='')
{
?>
<html >
<head> <title>Form</title></head>
<body>
<?php
if ($error != '') {
echo $error
}
if ($error2 != '') {
echo $error2;
}
if ($error3 != '') {
echo $error3;
}
?>
<form action="" method="post">
Name : <input type = "text" class = "form-control" name = "name_text" value="<?php echo $name; ?>"> <br/>
Tel :<input type = "text" class = "form-control" name = "tel_text" value="<?php echo $tel; ?>"> <br/>
Email :<input type ="text" class = "form-control " name = "email_text" value="<?php echo $email; ?>" > <br/>
<input name= "submit" type="submit" value="Update" class = "btn btn-primary" >
</form>
</body>
</html>
<?php
}
if (isset($_POST['submit'])){
$name = $_POST['name_text'];
$tel = $_POST['tel_text'];
$email = $_POST['email_text'];
if ($name== '' ){
$error = 'ERR: required field';
}
if(!is_numeric($telephone)){
$error2 = 'ERR: numbers only';
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$error3 = 'ERR: Email not valid';
}
else
{
***WILL PROCESS THE SQL QUERY ***
header("Location: main.php");
}
renderForm($name, $tel , $email ,$error, $error2, $error3);
}
else{
renderForm();
}
$con->close();
?>
<?php
include("conn/db.php");
function renderForm($name ='', $tel = '', $email ='', $error='', $error2='', $error3='')
{
?>
<html >
<head> <title>Form</title></head>
<body>
<?php
if ($error != '') {
echo $error
}
if ($error2 != '') {
echo $error2;
}
if ($error3 != '') {
echo $error3;
}
?>
<form action="" method="post">
Name : <input type = "text" class = "form-control" name = "name_text" value="<?php echo $name; ?>"> <br/>
Tel :<input type = "text" class = "form-control" name = "tel_text" value="<?php echo $tel; ?>"> <br/>
Email :<input type ="text" class = "form-control " name = "email_text" value="<?php echo $email; ?>" > <br/>
<input name= "submit" type="submit" value="Update" class = "btn btn-primary" >
</form>
</body>
</html>
<?php
}
if (isset($_POST['submit'])){
$name = $_POST['name_text'];
$tel = $_POST['tel_text'];
$email = $_POST['email_text'];
$is_valid = true;
if ($name== '' ){
$error = 'ERR: required field';
$is_valid = false;
}
if(!is_numeric($telephone)){
$error2 = 'ERR: numbers only';
$is_valid = false;
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$error3 = 'ERR: Email not valid';
$is_valid = false;
}
if($is_valid) {
***WILL PROCESS THE SQL QUERY ***
header("Location: main.php");
}
renderForm($name, $tel , $email ,$error, $error2, $error3);
}
else{
renderForm();
}
$con->close();
?>
Its just a small mistake:
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$error3 = 'ERR: Email not valid';
} else {
***WILL PROCESS THE SQL QUERY ***
header("Location: main.php");
}
You only checked the email and if it is corecct it was proceding. It did not include the other 2 checks for name and number.
I added a small variable to check if all 3 are correct.
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....
change like this
if (!$error) {
$_SESSION['firstName'] = $firstName;//error here
$_SESSION['lastName'] = $lastName;
header('Location: testing2.php');
exit();
}
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.
You have typo in your session name. You are assigning session to $_SESSION['fistName'] and trying to access it with $_SESSION['firstName'];
I'm trying to create a PHP file of a process form that indicates the required fields when processed. The code that I have is:
<html>
<head>
<style type="text/css">
.error{color: #FF0000;}
</style>
</head>
<body>
<?php
if(isset($_POST['fullname']) && $_POST['fullname'] != "") {
$fullname = $_POST['fullname'];
}
if(isset($_POST['email']) && $_POST['email'] != "") {
$email = $_POST['email'];
}
if(isset($_POST['feedback']) && $_POST['feedback'] != "") {
$text= $_POST['feedback'];
}
$nameErr = $emailErr = "";
$name = $email = "";
if ($_SERVER["REQUEST_METHOD"] == POST) {
if (empty($_POST["fullname"])){
$nameErr = "Name is required";
} else {
$name = test_input($_POST["fullname"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
}
?>
<h1>Customer Feedback</h1>
<p1>Please tell us what you think</p1><br><br>
<form method='POST' action='<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>' >
<p1>Your name:</p1><br>
<input type="text" name="fullname" value="<?php echo $fullname; ?>"><br><br>
<p1>Your email address:</p1><br>
<input type="text" name="email" value="<?php echo $email; ?>"><br><br>
<p1>Your feedback:</p1><br>
<textarea rows="5" cols="50" name="feedback"><?php echo $text;?></textarea><br><br>
<input type="submit" Value="Send Feedback"><br><br>
<?php
error_reporting(E_ALL);
$name = $_POST['fullname'];
$email = $_POST['email'];
$feed = $_POST['feedback'];
if (empty($name))
{
echo "Please enter your name, email and feedback.";
}
if (empty($email))
{
echo "Please enter your email and feedback.";
}
if (empty($feed))
{
echo "Please enter feedback.";
}
if (!empty($name) && !empty($email) && !empty($feed))
{
echo "You have inserted the correct data";
}
?>
</form>
</body>
</html>
However, when I run it on Chrome, I got a server error 500 saying that
The website encountered and error while retrieving process_4.php. It maybe down for maintenance or configured incorrectly.
The other PHP files that I've made leading up to this point have all worked correctly and I don't know why this one isn't.