Mailsnippet thank you load through AJAX

Hi,
I use the mailsnippet from Bastian on Gist.
My problem now is, how can I load the “thank you” or the URL here through AJAX?
Normally you set the jquery .ajax url to which the request is sent. But here is the action empty and the routing is completely realized through php if-requests. Also I have not idea what I have to do :).

Sorry for that nooby question but I’m really new to the ajax stuff and so please don’t hit me ;).

Use echo in your template.

<?php
  echo $alert;
?>

Then when you do the AJAX it will return the $alert as response. But you can do anything you want.

PS: Take my answer with a grain of salt though.

Hello Phil,

thanks for your reply. But I don’t understand how the alert will help me?
Normally I write a jQuery like that:

$(".selektor").submit(function() {

var url = "path/to/your/script.php"; // the script where you handle the form input.

$.ajax({
       type: "POST",
       url: url,
       data: $(".selektor").serialize(), // serializes the form's elements.
       success: function(data)
       {
           alert(data); // show response from the php script.
       }
     });

    return false; // avoid to execute the actual submit of the form.
});

I think my Problem is rather the part with

var url = "path/to/your/script.php";

Thanks

You can use Kirby’s r::ajax() method to check whether the request came from the form or your AJAX script (it returns true if it was an AJAX request). After processing the form data, you can then simply echo only the text you need instead of redirecting to another page:

// process the request data
// ...

if($email->send()) {
  if(r::ajax()) {
    echo 'Thank you!';
  } else {
    go('contact/thank-you');
  }
}

All I meant was that you can submit a response for the AJAX from your PHP using echo, as lukas demonstrates above.