thecodeholic / php-mvc-framework

PHP MVC Framework

Home Page:https://www.youtube.com/playlist?list=PLLQuc_7jk__Uk_QnJMPndbdKECcTEwTA1

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HOW TO INTEGRATE AJAX?

RobertoVasquez01 opened this issue · comments

I had fun integrating ajax and it works great. How would you do it?

1. I added to index.php:

$ app-> router-> post ('/ ajax', [AuthController :: class, 'ajaxMethod']);

2. In the AuthController.php

public function ajaxMethod (Request $ request) {
echo $ result;
}

3. In the view: register.php (... or any view, with some request)

   $ .ajax ({
       method: "POST",
       url: '/ ajax',
       data: { ... }
   })
   .done (function (result) {
         console.log (result);
   });

NOTE: Works great.
I am sending from the view the url: '/ ajax'. The router.php recognizes it, sends it to AuthController.php, executes the ajaxMethod () and responds without reloading the page. You will see the result on the console.

  • Any other input or observation?

  • Is there another alternative to ajax and not reloading the page in a request?

Great.
That's what you should do.
As an alternative to jQuery Ajax, you can use fetch API.

I appreciate. Thanks a lot.