I raise this question at the suggestion of @CamiloVasquez that has helped me in another question. I have a form that collects some data from the user, and has the option of sending it by mail via Ajax. Then the code that processes the data to assemble the mail.
<?php
$name = $_POST["nombre"];
$email = $_POST["correo"];
$cantHab = $_POST["cantHab"];
$cantAdu = $_POST["cantAdu"];
$cantMen = $_POST["cantMen"];
$edadesMenores = json_decode($_POST['edadesMenores']);
$destino = $_POST["desti"];
$EmailTo = "[email protected]";
$Subject = "Mail desde el Form";
// armo el cuerpo del mail
$Body = "Nombre: ";
$Body .= $name;
$Body .= "<br>";
...
$Body .= "Cantidad de Menores: ";
$Body .= $cantMen;
$Body .= "<br>";
// Punto del PROBLEMA
if($cantMen > 0 ){
echo ($Body .= "Edad Menor/es: <br>");
foreach($edadesMenores as $key => $edad){
echo( $Body .= echo $edad->menor . " = " . $edad->edad . "<br>" ;
}
echo($Body .= "<br>");
}
//armo los encabezados
$encabezados = "MIME-Version: 1.0\r\n";
$encabezados .= "Content-type: text/html; charset=iso-8859-1\r\n";
...
// envío el mail
$success = mail($EmailTo, $Subject, $Body, $encabezados);
// redirecciono
if ($success){
echo "success";
}else{
echo "invalid";
}
?>
The idea is that when there are no minors, it does not look like the title "Age Minor / is:". And if it does, there are 1 or more minors to know the age of each one.
Now, the mails arrive perfectly. Whether or not minors are
This is when there are no minors :
Name: Aguilera, Juan
Email: [email protected]
Destination: MAR DEL PLATA
Number of Rooms: 1
Number of Adults: 2
Number of Minors: 0
So when there are one or more children:
Name: Aguilera, Juan
Email: [email protected]
Destination: MAR DEL PLATA
Number of Rooms: 1
Number of Adults: 2
Number of Minors: 2
Age Minor / is:
Minor1 = 2
Minor2 = 5
The central point and that I am not seeing is that, being successful the sending of mail returns me the following message:
The problem is that it only appears when the number of minors is 0, if it is older, it sends the mail (that is, the sending would be successful), but this message does not appear. I suspect I'm missing something in the foreach, but I do not see it.
This is the ajax code.
$.ajax({
type: "POST",
url: "procesa-simulador.php",
data: {nombre: nombre, correo: correo, cantHab: cantHab, cantAdu: cantAdu, cantMen: cantMen,edadesMenores: JSON.stringify(edadesMenores), desti: desti},
success : function(text){
if (text == "success"){
formSuccess();
}
else {
formError();
}
}
});
}
function formSuccess(){
$( "#msgSubmit" ).removeClass( "hidden" );
}