Context Topic
This article discusses the process of sending an email containing the last form data sender's submitted data after the form data is sent to a MySQL database.
Key Concepts
- Form filling: A user interacts with a form, providing their information.
- Form data submission: The form data is sent to a server for processing.
- MySQL: A popular open-source relational database management system used for managing data.
- Email: A method of sending messages between users on the Internet.
Detailed Context
In this scenario, when someone fills out a form, the form data is sent to a server, where it is stored in a MySQL database. After the form data is stored, an email is sent to the last form data sender with the submitted data. The email's "To" field is set to the email address of the last form data sender.
Code Blocks
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get form data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Insert form data into MySQL database
$sql = "INSERT INTO MyForm (name, email, message)
VALUES ('$name', '$email', '$message')";
if ($conn->query($sql) === TRUE) {
// Get last inserted data's email
$sql = "SELECT email FROM MyForm ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$to_email = $row['email'];
}
// Email setup
$to = $to_email;
$subject = "Your form data has been submitted.";
$text = "Dear $name,
Your form data has been successfully submitted. Here is the information you provided:
Name: $name
Email: $email
Message: $message
Thank you for your submission.
Regards,
The Form Processor";
$headers = "From: [email protected]\r
Reply-To: [email protected]";
// Send email
mail($to, $subject, $text, $headers);
// Close connection
$conn->close();
- References: This article is based on personal knowledge and experience.