I want to send an email when a button has been clicked. The button is created with bootstrap 5. Jquery will then send an AJAX request to a php file which should send the email. But it looks like the requested email.php file is not called. No code in that file is executed, no exit.In the console I got the success message from ajax and 'Button 2 clicked'. I have checked several posts. What is wrong? Please see below code.
form.php
<div class="text-center mt-4 mb-4 ftp-order">
<button type="submit" name="ftp-submit" id="ftp-submit" class="btn btn-success"><?php echo $arr_lang['bestellen']; ?></button>
</div>
main.js
$('#ftp-submit').click(function (e) {
e.preventDefault();
console.log("Button clicked");
$.ajax({
url: 'dateien/ajax/premium_account.php',
data: '{pressed:pressed}',
datatype: 'JSON',
type: "POST",
success: function (data) {
console.log("Button clicked 2");
},
error: function (error) {
alert('There was some error');
console.log(error);
},
});
$('#ftp-alert').css("display", "block");
return false;
});
email.php
$sender = "sender@example.com";
if(mail('info@example.com', 'FTP', 'Test', $sender)) {
echo 'sent mail';
exit('exit1');
} else {
echo 'error sending email';
exit('exit2');
}
if (isset($_POST)) {
exit('exit');
?>
<script>console.log("received")</script>
<?php
}
?>
<div id="test"></div>
<script>console.log("received")</script>
Try to check your mail server it must be configured properly to mail() function be workable.