phpjqueryajaxmailer

AJAX is not triggering PHP mailer when submitting from iOS


dataString = 'param=stuff';
jQuery.ajax({
   cache: false,
   type: "POST",
   headers: { "cache-control": "no-cache" },
   url: "https://example.com/assets/mailer.php",
   data: dataString, 
   success: window.location.href = "https://example.com/thankyou"
});

This works fine with various browsers on Windows, MacOS, and Android. On iOS, the PHP mailer script isn't triggered, but the redirect works. Using Safari dev tools I can see there aren't any errors in the console. What am I missing?


Solution

  • success accepts a function, you are passing it a string while simultaneously assigning that string to window.location.href and navigating away.

    Use a function instead...

    jQuery
      .post("https://example.com/assets/mailer.php", { param: "stuff" })
      .done(() => {
        window.location.href = "https://example.com/thankyou";
      });
    

    Note that I've removed all the redundant options you were setting and used the safer method of encoding the request body.


    FYI you definitely don't need jQuery for this

    fetch("https://example.com/assets/mailer.php", {
      method: "POST",
      body: new URLSearchParams({ param: "stuff" }),
    }).then((res) => {
      if (res.ok) {
        window.location.href = "https://example.com/thankyou";
      }
    });