I need to make a text element that would generate an email to the admin on click.
I was looking for various solutions and came up with the following code that I compiled from various bits found on stackoverflow.
I don't know php, javascript, jquery, just html.
JQuery that I put before </head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.clicked').click(function() {
$.ajax({
type: "POST",
url: "/mail.php",
success: function(){
$('.clicked').fadeIn(1000);
}
});
return false;
});
});
</script>
PHP that I put in mail.php which is in public_html
<?php
if($_POST)
{
$to = 'mail@mail.com';
$subject = 'subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo 'Email Sent.';
}
?>
HTML
<span class="clicked">TEST</span>
Nothing happens.