Problem with value that Ajax returns when sending mail, even if it is successful

1

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" );
}
    
asked by Soluciones PyMES y Hogares 21.12.2017 в 18:12
source

1 answer

2

The problem is that Ajax requests recover here:

   success : function(text){

everything that is said ( echo ) on the server.

By having a lot of echo within the for , which for the rest is not useful at all in this case ... when the code enters that loop, it is not responding only success at the end , but it is responding what you have in those echo .

Then, when you evaluate here:

        if (text == "success"){
            formSuccess();

text is not equal to success but to everything you have put with echo , therefore, the condition is not met.

Solution

Remove all echo that you have within for :

 // Punto del PROBLEMA 
        if($cantMen > 0 ){
           $Body .= "Edad Menor/es: <br>";
            foreach($edadesMenores as $key => $edad){
                $Body .= $edad->menor . " = " . $edad->edad . "<br>" ;
            }
            $Body .= "<br>";
        }

With that it should work.

    
answered by 21.12.2017 / 18:36
source