6
I have the form to send/work perfectly with the other fields, but I cannot receive the checkbox selections. I need help to receive/manipulate in php:
THE HTML:
<div class="input-group">
<label for="servicos">NECESSITA EMBALAGENS?</label>
<div class="checkbox">
<label><input type="checkbox" value="1" name="servicos">Pelicula Aderente</label>
</div>
<div class="checkbox">
<label><input type="checkbox" value="2" name="servicos">Cartão Canelado</label>
</div>
<div class="checkbox">
<label><input type="checkbox" value="3" name="servicos">Caixa Cartão</label>
</div>
<div class="checkbox">
<label><input type="checkbox" value="4" name="servicos">Plástico Bolha</label>
</div>
</div>
O . JS :
$(function() {
$("input,textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var nome = $("#nome").val();
var email = $("#email").val();
var telefone = $("#telefone").val();
var origem = $("#origem").val();
var destino = $("#destino").val();
var data_prevista = $("#data_prevista").val();
var plano = $("#plano").val();
var arr = [];
$("input:checkbox[name=servicos]:checked").each(function() {
arr.push($(this).val());
});
var como = $("#como").val();
var descricao = $("#descricao").val();
var Nome = nome; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (Nome.indexOf(' ') >= 0) {
Nome = nome.split(' ').slice(0, -1).join(' ');
}
$.ajax({
url: "././mail/contact_me.php",
type: "POST",
dataType: 'json',
data: {
nome: nome,
email: email,
telefone: telefone,
origem: origem,
destino: destino,
data_prevista: data_prevista,
plano: plano,
servicos: arr,
como: como,
descricao: descricao
},
cache: false,
success: function(data) {
if (data.error) {
// 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>Prezado(a) " + Nome + ", algo não está certo. Por favor, tente novamente.");
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#orcamento').trigger("reset");
} else if (data.success) {
// 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>Prezado(a) " + Nome + ", sua mensagem foi enviada. Nós entraremos em contato consigo!</strong>");
$('#success > .alert-success').append('</div>');
//clear all fields
$('#orcamento').trigger("reset");
}
}
})
},
filter: function() {
return $(this).is(":visible");
},
});
});
/*When clicking on Full hide fail/success boxes */
$('#nome').focus(function() {
$('#success').html('');
});
PHP:
$nome = test_input($_POST['nome']);
$email = test_input($_POST['email']);
$telefone = test_input($_POST['telefone']);
$origem = test_input($_POST['origem']);
$destino = test_input($_POST['destino']);
$data_prevista = test_input($_POST['data_prevista']);
$plano = test_input($_POST['plano']);
$servicos = test_input($_POST['servicos']);
$como = test_input($_POST['como']);
$descricao = test_input($_POST['descricao']);
I think the code is all right, both html and js. The problem is the reception and creation of the variable or array (I don’t understand), in php. Help me?
1 - no menhum id called "servicos" in your html. 2 - identifying multiple tags with id does not work. id is unique, use class. 3 - After taking the values correctly why not go through the ajax data correctly? you take the values with var services1 and play on date as services?! 4 - why not use serialize?
– Gabriel Rodrigues
Gabriel, I’m a designer starting with web programming. I need help with this part.
– Bruno Luz