Butoon click mail in php not form
You want to send an email using PHP when a button is clicked, without using a form. This is possible using JavaScript and PHP. Here's an example:
HTML
<button id="send-email-btn">Send Email</button>
JavaScript
document.getElementById("send-email-btn").addEventListener("click", function() {
var email = "[email protected]"; // replace with the recipient's email
var subject = "Test Email"; // replace with the subject of the email
var body = "This is a test email sent using JavaScript and PHP."; // replace with the body of the email
// Create a new XMLHttpRequest object
var xhr = new XMLHttpRequest();
xhr.open("POST", "send-email.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("email=" + email + "&subject=" + subject + "&body=" + body);
});
PHP (send-email.php)
<?php
// Check if the request is a POST request
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the email, subject, and body from the request
$email = $_POST["email"];
$subject = $_POST["subject"];
$body = $_POST["body"];
// Send the email using PHP's mail function
$to = $email;
$headers = "From: Your Name <[email protected]>";
$message = $body;
mail($to, $subject, $message, $headers);
// Output a success message
echo "Email sent successfully!";
}
?>
In this example, when the button is clicked, the JavaScript code sends a POST request to the send-email.php
file with the email, subject, and body as parameters. The PHP code then receives these parameters and sends an email using the mail
function.
Note that this is just a basic example, and you should consider security and validation when sending emails using PHP. Additionally, this example assumes that you have a mail server set up on your server, and that the mail
function is configured correctly.