I am in the process of creating a website for my company, I don't have a lot of money but a lot of time. I bought my domain names, my web host etc. in short in the site that I create I want to integrate a contact form with JS AJAX PHP on an html website. I specify that I also use BULMA for formatting. it's kind of like Bootstrap. I show you what I did. email does not send to address. i dont know how to add imap or smtp. can you help me solve my problem please?;
my .html file named index.html :
<!-- body -->
<body>
<!-- contact -->
<div class="block">
<footer class="footer">
<h2 class="heading-site">Contactez-moi</h2>
<div class="footer-contact-form">
<form>
<div class="field">
<label class="label">Votre nom</label>
<div class="control">
<input id="name" class="input" type="text" placeholder="votre prénom" name="name">
</div>
</div>
<div class="field">
<label class="label">Votre Prénom</label>
<div class="control">
<input id="firstname" class="input" type="text" placeholder="votre nom" name="firstname">
</div>
</div>
<div class="field">
<label class="label">Votre email</label>
<div class="control">
<input id="email" class="input" type="text" placeholder="[email protected]" name="email">
</div>
</div>
<div class="field">
<label class="label">Votre message</label>
<div class="control">
<textarea id="message" class="textarea" placeholder="Décrivez votre entreprise et expliquez en quoi puis-je vous aider" name="message"></textarea>
</div>
</div>
</form>
<button class="button is-link" id="send_email">Envoyer !</button>
</div>
<div class="footer-informations">
<p>65 rue des peupliers</p>
<p>75015 Paris</p>
<ul>
<li>
<a href="#">
<i class="fab fa-facebook-square"></i>
</a>
</li>
<li>
<a href="#">
<i class="fab fa-twitter-square"></i>
</a>
</li>
</ul>
</div>
</footer>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="src/js/main.js"></script>
</body>
<!-- body -->
my .js file named main.js :
// send mail with ajax
$('#send_email').click(function(e){
e.preventDefault();
var data = {
email: $('#email').val(),
name: $('#name').val(),
firstname: $('#firstname').val(),
message: $('#message').val()
};
// AJAX
$.ajax({
url: "mail.php",
type: 'POST',
data: data,
success: function(data) {
$('#js_alert_success').css({'display' : 'block'});
setTimeout(function(){
$('#js_alert_success').css({'display' : 'none'});
$('#email').val("");
$('#name').val("");
$('#firstname').val("");
$('#message').val("")
}, 3000);
},
error: function(data) {
$('#js_alert_danger').css({'display' : 'block'});
setTimeout(function(){
$('#js_alert_danger').css({'display' : 'none'});
$('#email').val("");
$('#name').val("");
$('#firstname').val("");
$('#message').val("")
}, 3000);
}
});
});
my php file who named mail.php :
<?php
if($_POST){
$firstname = $_POST['firstname']
$email = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email>\r\nReply-to : $name <$email>\nX-Mailer:PHP";
$subject="demande de renseignement";
$destinataire="[email protected]";
$body="$message";
if(mail($destinataire,$subject,$body,$headers)) {
$response['status'] = 'success';
$response['msg'] = 'your mail is sent';
} else {
$response['status'] = 'error';
$response['msg'] = 'Something went wrong';
}
echo json_encode($response);
}
?>
I
Please follow the below example of mail sending and let me know if you have issue understanding any part of it
PHP section
HTML Section