How to use AJAX with Slim 3?

Hello there I am wondering on how to use AJAX with Slim 3 I tried myself but can’t figure it out here is the example I am working on please help me

I have a users table in the database it has a lot of users info I want to retrieve the user name and salary back so I created a UserController that will query the Database and get these data by the help of the UserModel once these data is back the Controller would then pass it down to the view which finally would display them in a table each user and his Salary.

The only problem is when a new user is added to the database it will not appear in the view that renders the users unless I recall the route I mean refresh the page.

so please how can I use AJAX to bring the new added users and display them automatically Thank you guys hope you get what I mean and can help me.

There isn’t anything particularly special about using AJAX within Slim, are you familiar with using it outside of Slim? Otherwise, in your example you don’t necessarily need AJAX to make that happen, although you may want to. After adding the new user to the database, you can redirect back to the view you were on before which will reload the page for you and grab the new information from the database.

return $response->withRedirect('your_previous_path);

yes I know this but I don’t want to do it this way you may understood me because of this silly example I gave let me tell you what I will do eventually in the real website.

I want to make a coupons website so there will be a page for customers to view the coupons and I as admin will have a page to add coupons so I don’t want my customer t refresh the page to see new coupons as I add them. I want it to be something like twitter feed when there is a new tweet there is a div appears tills the user that there is a new coupons click to load.

hope you understand me now

I understand. There isn’t anything unique to Slim and using AJAX. Often instead of returning a full view (with something like Twig) you might submit the form (perhaps using jQuery to send the request) to a controller that adds the record and returns the created record as JSON. Your Javascript (again, perhaps via jQuery) would parse the response and add the new div.

So from your Slim controller you might…

$coupon = ['coupon_name' => 'My Coupon', 'coupon_discount' => '20%'];
return $response->withJson($coupon);

1 Like

I think what you’re looking for is real-time push data. If that’s the case you’d want to look into something like NodeJS or PHP Web Sockets. Google Firebase might have this functionality as well.

1 Like

Yeah Thank you I found it the problem was that I was struggling on how to send the request to the controller from JQuery but after I calm down and think I remembered that if I send the JQuery request to the route then the route will pass it to the controller hahahahh lol I feel really stupid now

Yeah Thank you I know this but its a small project there is no need for web sockets thanks for helping