Welcome Guest, Not a member yet? Register   Sign In
How to implement EventSource on Codeignigter app?
#16

(02-24-2020, 07:33 PM)dave friend Wrote:
(02-24-2020, 05:39 PM)SmokeyCharizard Wrote: So I just tried your code out, and when I submit the form, a white page loads with the timestamp being loaded periodically.
     data: This is a message at time 2020-02-24T19:49:21-0500

     data: This is a message at time 2020-02-24T19:49:25-0500

     data: This is a message at time 2020-02-24T19:49:29-0500

     And so on.

This is good, but I want these tamestamps to be added to the original page, the one with the form, and for these timestamps to be updates the appeared beneath the submit button. It seems to be returning a new page instead...

Again, thanks for your help, Dave! Do you think you might know what is going wrong?

Do you understand that when you submit a form the action URL (i.e. c_controller/classicFirearmByDate) is a redirect? That's why the page turns white. You have sent the browser to another controller.

The easiest fix requires more JavaScript. You need to catch the submit event and stop the normal HTML process by preventing the browser from doing the event. Then you use javascript to POST the data using the Fetch API. The argument to Fetch should be the action that you used for the form.

By doing all that you don't leave the page where you want to display the messages from EventSource.

Remove the $this->load->view(...) lines from C_controller::classicFirearmByDate()

As I said very early on in this thread, if you want to go to another page when processing is done, you will have to use javascript. This probably means redirecting from the Promise that Fetch returns.  All that said, I have never combined Fetch and EventSource so I don't know what, if any, problems using them at the same time might cause. Happy Experimenting!

Holy cow, thank you an absolute megaton, David! I'm working on implementing fetch now, but there seem to be a few caveats. How do I implement fetch in this instance? You've helped me a bunch, but can you help with this one last thing? Thanks!
Reply


Messages In This Thread
RE: How to implement EventSource on Codeignigter app? - by SmokeyCharizard - 02-24-2020, 08:48 PM



Theme © iAndrew 2016 - Forum software by © MyBB