Edrol97
Silver Coder
Hi all, am very new and inexperienced with PHP. I have this code for submitting a form to email that I asked ChatGPT to write (may or may not be a fatal flaw), and I can't seem to get it to work. Any and all help appreciated. Note, unless it's not obvious from what I've said, I'm a complete novice. Thanks, Eliot.
PHP:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Collect form data and sanitize it
$name = htmlspecialchars(trim($_POST['name']));
$email = htmlspecialchars(trim($_POST['email']));
$telephone = htmlspecialchars(trim($_POST['telephone']));
$piece = htmlspecialchars(trim($_POST['piece']));
$comments = htmlspecialchars(trim($_POST['comments']));
if (!empty($name) && !empty($email) && !empty($telephone) && !empty($piece)) {
echo "<h2>Form Submission Successful!</h2>";
echo "<p><strong>Name:</strong> $name</p>";
echo "<p><strong>Email:</strong> $email</p>";
echo "<p><strong>Telephone:</strong> $telephone</p>";
echo "<p><strong>Piece</strong> $piece</p>";
echo "<p><strong>Size:</strong> $comments</p>";
} else {
echo "<h2>Error: Please fill out all required fields.</h2>";
}
} else {
echo "<h2>Error: Invalid request method.</h2>";
}
$to = "[email protected]"; // Replace with your actual email address
$subject = "New Form Submission";
$message = "
Name: $name\n
Email: $email\n
Telephone: $telephone\n
Piece: $piece\n
Size: $comments
";
$headers = "From: $email"; // Sender's email
// Send the email
if (mail($to, $subject, $message, $headers)) {
echo "<h2>Thank you! Your form has been submitted successfully.</h2>";
} else {
echo "<h2>Sorry, there was an issue sending your form. Please try again later.</h2>";
}
} else {
echo "<h2>Error: Please fill out all required fields.</h2>";
}
} else {
echo "<h2>Error: Invalid request method.</h2>";
}
?>
Last edited: