I have a contact form template that I use on my sites. It uses JS to validate the content of the fields and PHP to send the data to email. I would like to know if it is possible to send attached files (images) using this template. I thank you all at once. I leave the codes here:
HTML FORM:
<form name="sentMessage" id="contactForm" novalidate>
<div class="control-group form-group">
<div class="controls col-lg-4">
<label>Nome Completo*</label>
<input type="text" class="form-control" id="name" required data-validation-required-message="Por favor, insira seu nome.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls col-lg-3">
<label>Telefone*</label>
<input type="tel" class="form-control" id="phone" required data-validation-required-message="Por favor, insira seu número de telefone." placeholder="(00) 0000-0000">
</div>
</div>
<div class="control-group form-group">
<div class="controls col-lg-5">
<label>Email*</label>
<input type="email" class="form-control" id="email" required data-validation-required-message="Por favor, insira seu email.">
</div>
</div>
</div><!-- row -->
<hr>
<div class="row">
<div class="control-group form-group">
<div class="controls col-lg-3">
<div class="form-group">
<label style="padding-top:15px" for="sel1">Assunto:</label>
<select class="form-control" id="sel1">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</div>
</div>
</div>
</div><!-- row -->
<br>
<hr>
<div class="row">
<div class="control-group form-group">
<div class="controls col-lg-12">
<label>Mensagem</label>
<textarea rows="10" cols="100" class="form-control" id="message" maxlength="999" style="resize:none"></textarea>
</div>
</div>
</div><!--row-->
<div class="row">
<div class="col-lg-5">
<div id="success"></div>
<!-- For success/fail messages -->
<br>
<button type="submit" class="btn btn-success btn-lg">Enviar</button>
</div>
</form>
</div>
JAVASCRIPT TO VALID:
$(function() {
$("input,textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
},
submitSuccess: function($form, event) {
event.preventDefault();
var name = $("input#name").val();
var phone = $("input#phone").val();
var email = $("input#email").val();
var sel1 = $("input#sel1").val();
var message = $("textarea#message").val();
var firstName = name;
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
$.ajax({
url: "bin/contact_me.php",
type: "POST",
data: {
name: name,
phone: phone,
email: email,
sel1: sel1,
message: message
},
cache: false,
success: function() {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Sua mensagem foi enviada. </strong>");
$('#success > .alert-success')
.append('</div>');
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append("<strong>" + firstName + " Parece que não está funcionando...</strong> Você poderia enviar um email direto? Desculpe pela inconveniência.");
$('#success > .alert-danger').append('</div>');
$('#contactForm').trigger("reset");
},
})
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});});
$('#name').focus(function() { $('#success').html(''); });
PHP TO SUBMIT:
<?php
if(empty($_POST['name']) ||
empty($_POST['phone']) ||
empty($_POST['email']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email_address = $_POST['email'];
$sel1 = $_POST['sel1'];
$message = $_POST['message'];
$to = '[email protected]';
$email_subject = "Formulário de contato, $name te enviou uma mensagem.";
$email_body = "Você recebeu uma nova mensagem do site.\n\n"."Aqui estão os detalhes:\n\nNome: $name\n\nTelefone: $phone\n\nAssunto: $sel1\n\nEmail: $email_address\n\nMensagem:\n$message";
$headers = "From: $email_address\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>