JQUERY响应不起作用

时间:2016-02-06 22:44:48

标签: javascript php jquery ajax email

我有一个ajax发送一封包含一些信息的电子邮件,然后回复已发送或未发送,我的问题是我不知道为什么aj之后的.done应该做什么如果响应被“发送”根据检查员发送了回复,但它总是转到我的if iftion中的其他地方,我不知道为什么,发送电子邮件,除了我不能理解为什么总是去其他地方。

我的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;

2 个答案:

答案 0 :(得分:0)

是的,发送电子邮件,'respuesta'是我从php获取数据的地方,应该是“发送”或“未发送”来自if(mail())如果它是真的返回“发送”哪个根据检查员工作我得到“发送”作为答案,但仍然当我这样做(respuesta ==“发送”) 做点什么

否则 做点什么

如果php的答案是“发送”

,那么为什么它总是会转到其他地方

答案 1 :(得分:0)

您可能需要使用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;
                });
            });
            });