What are the common mistakes in the provided PHP code related to form submission and actions?
The common mistakes in the provided PHP code related to form submission and actions are: 1. Not checking if the form has been submitted before processing the form data. 2. Not properly sanitizing and validating user input, which can lead to security vulnerabilities. 3. Not redirecting the user after form submission to prevent form resubmission on page refresh. To fix these issues, you should check if the form has been submitted using the isset() function, sanitize and validate user input using functions like htmlspecialchars() and filter_var(), and redirect the user after form submission using the header() function.
<?php
if(isset($_POST['submit'])){
// Sanitize and validate form data
$name = htmlspecialchars($_POST['name']);
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
// Process form data
// Your code to process form data goes here
// Redirect user after form submission
header("Location: thank-you.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Form Submission</title>
</head>
<body>
<form method="post" action="">
<input type="text" name="name" placeholder="Name">
<input type="email" name="email" placeholder="Email">
<button type="submit" name="submit">Submit</button>
</form>
</body>
</html>