How to add two forms details in one mail php
To add two forms' details in one mail using PHP, you can use the following steps:
Step 1: Create two forms
Create two HTML forms with different names and action attributes. For example:
<!-- Form 1 -->
<form action="sendmail.php" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<input type="submit" value="Submit">
</form>
<!-- Form 2 -->
<form action="sendmail.php" method="post">
<label for="message">Message:</label>
<textarea id="message" name="message"></textarea><br><br>
<input type="submit" value="Submit">
</form>
Step 2: Create a PHP script to send the mail
Create a PHP script (e.g., sendmail.php
) that will receive the form data and send an email with both forms' details. Here's an example:
<?php
// Configuration
$to = '[email protected]';
$subject = 'Form Submission';
// Get form data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Create the email body
$body = "Form 1 Details:\n";
$body.= "Name: $name\n";
$body.= "Email: $email\n\n";
$body.= "Form 2 Details:\n";
$body.= "Message: $message\n";
// Send the email
$headers = 'From: [email protected]'. "\r\n";
$headers.= 'Content-type: text/plain; charset=UTF-8'. "\r\n";
mail($to, $subject, $body, $headers);
// Redirect to a thank-you page
header('Location: thank-you.php');
exit;
?>
Step 3: Test the script
Test the script by submitting both forms and verifying that the email is sent with both forms' details.
Tips and Variations
- You can use a single form with multiple fields and use the
$_POST
array to access the form data. - You can use a PHP library like PHPMailer to send the email with more advanced features.
- You can add more forms or fields to the script as needed.
- You can also use a database to store the form data and send the email with the stored data.