在jQuery.ajax成功处理程序中使用来自PHP脚本的返回值的问题

时间:2022-10-08 07:40:51

I'm getting a weird result from a jQuery ajax request sending form details to a PHP script. The same scripts are used elsewhere problem free. Basically the form is submitted using jQuery.ajax like this:

我从jQuery ajax请求发送表单详细信息到PHP脚本得到一个奇怪的结果。其他地方使用相同的脚本无问题。基本上,表单是使用jQuery.ajax提交的,如下所示:

//if submit button is clicked
$('#form1').submit(function () {    

    //Get the data from all the fields
    var name = $('input[name=name]');
    var email = $('input[name=email]');
    var con_email = $('input[name=con_email]');
    var comments = $('textarea[name=comments]');

    //organize the data properly
    var data = 'name=' + name.val() + '&email=' + email.val() + '&con_email=' + con_email.val() + '&comments='  + encodeURIComponent(comments.val());


    //show the loading sign
    $('.loading').show();

    //start the ajax
    $.ajax({
        //this is the php file that processes the data and send mail
        url: "process-email/process.php",   

        //GET method is used
        type: "GET",

        //pass the data         
        data: data,     

        //Do not cache the page
        cache: false,

        //success
        success: function () {              
            //if process.php returned 1/true (send mail success)
            if (html==1) {                  
                //hide the form
                $('.form').fadeOut('slow');                 

                $('.done').delay(1000).fadeIn('slow');

            }               
        }       
    });



    //cancel the submit button default behaviours
    return false;
});

The PHP script works fine, the email is sent and 1 is returned (email sent) but the script stops at: if(html==1). I get this error

PHP脚本工作正常,发送电子邮件并返回1(发送电子邮件),但脚本停在:if(html == 1)。我收到这个错误

html is not defined

As said above exactly the same script works fine somewhere else, but here I get that error and the script is stopped. Can someone please help to understand where there might be the problem?

如上所述,完全相同的脚本在其他地方工作正常,但在这里我得到了该错误并且脚本停止了。有人可以帮助了解可能存在问题的地方吗?

3 个解决方案

#1


1  

You have to add the parameter reference:

您必须添加参数引用:

success: function (html) {
    //if process.php returned 1/true (send mail success)
    //.....
}

Then you can use this parameter, which will be the response from server.

然后您可以使用此参数,这将是来自服务器的响应。

#2


1  

Check your success funcion, should be like:

检查你的成功功能,应该是这样的:

success: function (response) {              
            //if process.php returned 1/true (send mail success)
            if (response == "1") {                  
                //hide the form
                $('.form').fadeOut('slow');                 

                $('.done').delay(1000).fadeIn('slow');

            }               
        }     

#3


1  

It looks like you are not returning the response from the PHP script to the JavaScript function. If you do something like the following for your success function it should get you on the right track:

看起来您没有将PHP脚本的响应返回给JavaScript函数。如果您为成功功能执行以下操作,则应该让您走上正确的轨道:

success: function( html )
{
    if(html=='1')
    {
        [...]
    }
}

#1


1  

You have to add the parameter reference:

您必须添加参数引用:

success: function (html) {
    //if process.php returned 1/true (send mail success)
    //.....
}

Then you can use this parameter, which will be the response from server.

然后您可以使用此参数,这将是来自服务器的响应。

#2


1  

Check your success funcion, should be like:

检查你的成功功能,应该是这样的:

success: function (response) {              
            //if process.php returned 1/true (send mail success)
            if (response == "1") {                  
                //hide the form
                $('.form').fadeOut('slow');                 

                $('.done').delay(1000).fadeIn('slow');

            }               
        }     

#3


1  

It looks like you are not returning the response from the PHP script to the JavaScript function. If you do something like the following for your success function it should get you on the right track:

看起来您没有将PHP脚本的响应返回给JavaScript函数。如果您为成功功能执行以下操作,则应该让您走上正确的轨道:

success: function( html )
{
    if(html=='1')
    {
        [...]
    }
}