0

I am not so good at JavaScript nor jQuery, but I try to learn it. I am creating a contact form with ajax sending, honeypot and fancybox. I am almost done, but I have feeling, that event success in ajax does not work. Here is part of code:

 $.ajax({
    type: 'POST',
    url: '../data/sendmessage.php',
    data: $("#contactForm").serialize(),
    success: function (data) {
        if (data === "true") {
        $("#contactForm").fadeOut("fast", function () {
            $(this).before("<p><strong>Thank you for message</strong></p>");
            setTimeout("$.fancybox.close()", 1000);
        });
        }
    }
    });

I thought, it will show the message and then close fancybox, but it doesn't. Can somebody help me to understand, what am I doing wrong? ...I hope this part of code is enough, if not, I will be glad to add whole the script, but that should work OK.

EugenSunic
  • 13,162
  • 13
  • 64
  • 86
PeterB
  • 59
  • 9
  • **Never** pass a string to `setInterval()` or `setTimeout()`. Doing so is as bad as using `eval()` and it results in unreadable and possibly insecure code as soon as you use variables since you need to insert them into the string instead of passing the actual variable. The proper solution is `setInterval(function() { /* your code *) }, msecs);`. The same applies to `setTimeout()`. If you just want to call a single function without any arguments, you can also pass the function name directly: `setInterval(someFunction, msecs);` (note that there are **no** `()` behind the function name) – ThiefMaster Apr 19 '15 at 22:12
  • Why are you using "===" ? are you sure you are returning a string from the server? did you check if your condition is true? – Ziv Weissman Apr 19 '15 at 22:18
  • ...that is right I did not get true, but how do I check if sending was successful ? ...I am using === because it is same as == but it also check, if data type is same... – PeterB Apr 19 '15 at 22:29
  • @PeterB what do you mean with _how do I check if sending was successful ?_? Success function is called just when ajax request is done with success.. – DrKey Apr 19 '15 at 22:49
  • ...ach OK, then I got it. Thanks a lot to all of you. – PeterB Apr 19 '15 at 22:59
  • If you are calling this function from a form button, are you 100% sure that you are blocking the default form submission. If you aren't, then your page is reloading and that's why your code is cut short and the `success` doesn't execute. See [this answer](http://stackoverflow.com/questions/27559549/javascript-not-proceeding-aynchronously-beyond-ajax-call/27559675#27559675) for a description of someone else having this problem to see if it might also apply to you. – jfriend00 Apr 20 '15 at 00:10
  • Also, it goes without saying that you should create an `error` handler for your ajax call and log any error if that is getting hit and you should report anything that the debug log shows in the browser in case there are some sort of errors there. And, also put a `console.log()` before your `if (data === "true")` to see if that gets hit in case your `if` statement is why you're missing it. – jfriend00 Apr 20 '15 at 00:12

0 Answers0