2

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:

//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

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?

1
  • "organize the data properly" — you aren't, the data needs escaping. Just construct an object and use that instead of a string and jQuery will do it for you. I'd show you how … but "Get the data from all the fields" means you should just be using jQuery('form#myForm').serialize(). Commented Jan 3, 2012 at 13:16

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

sorry got you. I will try now
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');

            }               
        }     

Comments

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:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.