Update part of rendered view from backend |
Hello
Suppose i have some data in backend that has changed for some reason. I have rendered view and now i wont to update only certain part of that view according to this new data. How is it possible to accomplish this kind of task? Thank you
You would need to use jQuery or JavaScript Ajax to do it.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(08-22-2020, 01:38 PM)InsiteFX Wrote: You would need to use jQuery or JavaScript Ajax to do it.Yes But in this case the "initiator" of update is client. I want the initiator to be server, not the client: something happens on server side, like some event gets fired, the view should change accordingly. This my controller home.php: <?php namespace App\Controllers; use CodeIgniter\Events\Events; class Home extends BaseController { public function index() { echo view('test_events'); } public function testEvent(){ Events::trigger('kukuni'); } //-------------------------------------------------------------------- } This is my test_events.php file: <?php use CodeIgniter\Events\Events; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Welcome to CodeIgniter 4!</title> <meta name="description" content="The small framework with powerful features"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="shortcut icon" type="image/png" href="/favicon.ico"/> </head> <body> <?php Events::on('kukuni', function() { echo "jjjjjjjjjjjjjjjjjjjj" ; }); ?> </body> </html> Let me clarify even more what i want to achieve: I have firm knowledge, how to make for example Ajax calls with JavaScript from client side to server and update Dom and JavaScript data according to returned data. But in this case the "initiator" of update is client. I want the initiator to be server, not the client: something happens on server side, like some event gets fired, the view should change accordingly. In the example i provided, i tried to rise event `kukuni` inside `testEvent()` controller function and handle that event inside `test_events.php` which should `echo` some random string but browser page does not get updated with this random string. I also prefer not to use any sort of manual polling in regular intervals from client to server. Is this kind functionality feasible with CodeIgniter? What am i missing and/or doing wrong?
The only way it can be done is using JavaScript if is sent to the CodeIgniter app then you would
need to have CodeIgniter send it to ajax and ajax update the view. Events What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
What you need to do are set up an regular Timing Event to ask CodeIgniter if there are any new data. A server can't communicate with the client.
https://www.w3schools.com/js/js_timing.asp
I understand
After more research i found out that i was after websockets functionality. I am working on that now, but if it did not work, i will use the approach described here. Maybe you could recommend correct way to enable websockets functionality in codeigniter? Thank you very much |
Welcome Guest, Not a member yet? Register Sign In |