I've just been going through this Traversy Media PHP crashcourse thing and trying to implement it into a project of my own. Right now, I'm basically validating the data and when I hit submit, all the info typed into the fields reset and go blank. In the linked video, he's talked about using the value in the HTML to fix the issue and so far, it works. The only problem is the textarea always goes blank. I have no idea why it works for everything else but not that particular element. What have I missed?
<?php
// Message Vars
$msg = '';
$msgClass = '';
// Check for Submit
if(filter_has_var(INPUT_POST, 'submit')){
// get form data
$name = htmlspecialchars($_POST['name']);
$cname = htmlspecialchars($_POST['cname']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
// check required fields
if(!empty($email) && !empty($name) && !empty($message)){
// passed
//check emails
if(filter_var($email, FILTER_VALIDATE_EMAIL)=== false){
// failed
$msg = 'Please use a valid email';
$msgClass = 'error-fail';
} else {
//Passed
echo'passed';
}
} else {
// Fail
$msg = 'Please fill in all the required fields';
$msgClass = 'error-fail';
}
}
?>
<!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>Ravenfell</title>
<link rel="stylesheet" href="./css/main.css" />
<link rel="stylesheet" href="./css/contact.css" />
</head>
<body>
<section id="sec5" class="lt-bg dk-txt desktop tablet">
<div class="content-wrapper flex-container">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="form-container">
<div class="form-head">
<h2>Connect with us</h2>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Exercitationem, expedita.</p>
</div>
<!-- Fullname -->
<p class="col1">Full Name:</p>
<input type="text" name="name" placeholder="Your Name" class="col2" value="<?php echo isset($_POST['name']) ? $name : ''; ?>">
<!-- Company Name -->
<p class="col1">Company Name <span class="optional">(optional)</span>:</p>
<input type="text" placeholder="Company Name" name="cname" class="col2" value="<?php echo isset($_POST['cname']) ? $cname : ''; ?>">
<!-- email -->
<p class="col1">Email:</p>
<input type="email" name="email" placeholder="example@company.com" class="col2" value="<?php echo isset($_POST['email']) ? $email : ''; ?>">
<!-- Message -->
<p class="col1">Message:</p>
<textarea name="message" cols="30" rows="10" placeholder="Message" value="<?php echo isset($_POST['message']) ? $message : ''; ?>"></textarea>
<!-- Button and Errors -->
<?php if($msg != ''): ?>
<div class="alert <?php echo $msgClass ?>"><?php echo $msg ?></div>
<?php endif; ?>
<button type="submit" name="submit" class="submit-btn">Submit →</button>
</form>
</div>
</section>
</body>
</html>