JQUERY响应不起作用


JQUERY response not working

>我有一个ajax,它发送了一封包含一些信息的电子邮件,然后回复发送或不发送,我的问题是我不知道为什么在.done之后的ajax应该做一些事情,如果响应是根据检查器"发送"的,响应被发送,但它总是转到我的if发送中的其他, 我不知道为什么,电子邮件被发送了,所有内容,但我只是不明白为什么总是去其他。

我的jquery是这样的:

function Send()
{
    var nom = $('#name').val();
    var ape = $('#lastname').val();
    var occ = $('#occupation').val();
    var dep = $('#department').val();
    var dob = $('#datob').val();
    var bdp = $('#place').val();
    var add = $('#address').val();
    var cid = $('#cidnumber').val();
    var pho = $('#phone').val();
    var pem = $('#mail').val();
    var sta = $('#station').val();
    if(bdp == '' || add == '' || cid == '' || pho == '' || pem == '')
    {
        swal("Ooops!", "All the information MUST be filled, Please check!.", "error");
    }
    else
    {
        $('#myModalEmail').modal('toggle');
        $.ajax({
            url: 'AccionesPHP.php',
            type: 'POST',
            data: {funcion:'SendIdMail',name:nom,lastname:ape,occupation:occ,department:dep,dob:dob,place:bdp,address:add,companyid:cid,phone:pho,mail:pem,station:sta}
        })
        .done(function(respuesta){
            if(respuesta == "sent")
            {
                $('#myModalEmail').modal('hide');
                swal("Success!", "Your Card Request has been sent successfuly.", "success");
                LoadData();
            }
            else
            {
                swal("Ooops!", "This is embarrasing, we have a proble could you try later, Error!.", "error");
            }
        });
    }
}

我的PHP是这样的:

$name = $_POST['name'];
        $lastname = $_POST['lastname'];
        $occupation = $_POST['occupation'];
        $department = $_POST['department'];
        $dob = $_POST['dob'];
        $place = $_POST['place'];
        $address = $_POST['address'];
        $companyid = $_POST['companyid'];
        $phone = $_POST['phone'];
        $mail = $_POST['mail'];
        $station = $_POST['station'];
        //ENVIO DE MAIL PARA DAVID WING
        $mensaje = '<html>'; 
        $mensaje .= '<body>';
        $mensaje .= '<h3> New Company Identification Card Request </h3>';
        $mensaje .= '<div><p> The information for the Card request is below:</p>';
        $mensaje .= '<label>Name</label>';
        $mensaje .= '<input type="text" value="'. $name .'" disabled><br<br>';
        $mensaje .= '<label>Last Name</label>';
        $mensaje .= '<input type="text" value="'. $lastname .'" disabled><br<br>';
        $mensaje .= '<label>Occupation</label>';
        $mensaje .= '<input type="text" value="'. $occupation .'" disabled><br<br>'; 
        $mensaje .= '<label>Depatment</label>';
        $mensaje .= '<input type="text" value="'. $department .'" disabled><br<br>'; 
        $mensaje .= '<label>Date of Birth</label>';
        $mensaje .= '<input type="text" value="'. $dob .'" disabled><br<br>';
        $mensaje .= '<label>Birth Place</label>';
        $mensaje .= '<input type="text" value="'. $place .'" disabled><br<br>';
        $mensaje .= '<label>Address</label>';
        $mensaje .= '<input type="text" value="'. $address .'" disabled><br<br>';
        $mensaje .= '<label>Company ID</label>';
        $mensaje .= '<input type="text" value="'. $companyid .'" disabled><br<br>';
        $mensaje .= '<label>Phone</label>';
        $mensaje .= '<input type="text" value="'. $phone .'" disabled><br<br>';
        $mensaje .= '<label>Personal e-mail</label>';
        $mensaje .= '<input type="text" value="'. $mail .'" disabled><br<br>';
        $mensaje .= '<label>Station</label>';
        $mensaje .= '<input type="text" value="'. $station .'" disabled><br<br>';
        $mensaje .= '</body> </html>';
        $para = 'gjmm1711@gmail.com';
        $titulo = 'New Company Identification Card Request';
        $cabeceras = 'MIME-Version: 1.0' . "'r'n";
        $cabeceras .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
        $cabeceras .='From: webmaster@example.com' . "'r'n" .
                    'Reply-To: webmaster@example.com' . "'r'n" .
                    'X-Mailer: PHP/' . phpversion();
        if(mail($para, $titulo, $mensaje, $cabeceras))
        {
            print "sent";
        }
        else
        {
            print "notsent";
        }
        break;

是的,电子邮件已发送,"respuesta"是我从 php 获取数据的地方,这些数据应该是"发送"或"不发送",它来自 if(邮件 ()) 如果它是真的,则返回"已发送",根据检查器,我得到"发送"作为答案,但当我这样做时仍然 if (respuesta=="发送")做点什么

还做点什么

Idk 为什么如果 php 的答案是"发送"的,它总是去别的

您可能需要为此使用 Promise,因为您的 ajax 查询正在查询另一台服务器。这应该可以稍微调整一下您的四个需求。

喜欢这个;

   function send_mail(nom,ape,occ, ...){
         return new Promise(function (resolve, reject) {
            $.ajax({
            url: 'AccionesPHP.php',
            type: 'POST',
            data
{'name':nom,'lastname':ape,'occupation':occ,'department':dep,'dob':dob,'place':bdp,'address':add,'companyid':cid,'phone':pho,'mail':pem,'station':sta},
            success: function (data, status) {
                resolve(data);
                return;
            },
            error: function (xhr, desc, err) {
                console.log(xhr);
                console.log("Details: " + desc + "'nError:" + err);
                return;
            }
        }); 
     });
    }

现在你的 ajax 事件处理程序

      $( document ).ready(function() {
         $('#myModalEmail').modal('toggle' , function (event) {
                event.preventDefault(event);
                send_mail().then(function (response) {
                 respuesata = response;
 if(respuesta == "sent")
            {
                $('#myModalEmail').modal('hide');
                swal("Success!", "Your Card Request has been sent successfuly.", "success");
                LoadData();
            }
            else
            {
                swal("Ooops!", "This is embarrasing, we have a proble could you try later, Error!.", "error");
            }
    //Check your data here
                }, function (error) {
                    console.error("Échec !", error);
                    return;
                });
            });
            });