How to handle form request form in modal window with AJAX request

I have a page with a modal window, that contains a contact form
I want to find the way how to process AJAX request from this form


see this thread:

1 Like


For the <form>, I use $this->grav[‘page’]->slug() for the action attribute and add a hidden field with a name unique to my contactform.

On submit of the form I check if the name of the hidden field isset in $_POST.

The reason I don’t use an arbitrary value for the action attribute is that if Javascript is not handling the form for some reason and the page is not submitted through Ajax, a 404 error will be thrown.

Note: I’m not using Grav’s forms, but custom bootstrap v4+ forms.


<form action="{{ contactForm.action }}" method="post" id="contactform">
        <input type="hidden" name="{{ }}">


if (isset($_POST[self::FORMNAME]) {
   $this->result = $this->handleForm();

   if ($this->isAjaxRequest()) {
      echo json_encode($this->result);
// continue page creation if not Ajax request
1 Like