Welcome Guest, Not a member yet? Register   Sign In
Ajax Sidebar Form on page - which controller or controller/method to use
#1

[eluser]Unknown[/eluser]
I'm working on a website homepage, and the homepage is going to have a little sign-up form in the sidebar of the page to let someone submit a name and email to join a mailing list.

I want to handle the form using Ajax, so that the form can be validated, and submitted without a page refresh.


There is a homepage controller which is loading the homepage view.

Here's my question.

When the form is submitted using Ajax, should I have the form get processed by a new method in my homepage controller (homepage/mailing_list)

or should I create a whole new independent controller (mailing_list.php) that would handle the Ajax request.
This new controller could do the validation and send the email. Can you have a controller like that - that will just handle the Ajax request, but not load any views?

Is one way better than the other, or preferred?

I know when I've done something similar in a non-Codeigniter site I have just used specific php file to process the Ajax request. But I'm not sure in the context of Codeigniter what needs to be done in the context of the MVC framework.


#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

[quote author="tom756" date="1368299014"]
When the form is submitted using Ajax, should I have the form get processed by a new method in my homepage controller (homepage/mailing_list)

or should I create a whole new independent controller (mailing_list.php) that would handle the Ajax request. [/quote]

By placing your subscription method into a new controller, you are making things more modular. It'll make your code easier to follow and maintain. I suspect that there will be more methods that will fall into the same category too, so to me, it makes sense to put the method into a new controller.

[quote author="tom756" date="1368299014"]This new controller could do the validation and send the email. Can you have a controller like that - that will just handle the Ajax request, but not load any views?[/quote]

Yes! MVC is a pattern, not a law. There are times when you simply want to echo a response for an AJAX request, and using a view doesn't make sense.

Even if you put all of your controller logic into a single controller (which I wouldn't recommend), you'd still be adhering to the MVC pattern.

[quote author="tom756" date="1368299014"]Is one way better than the other, or preferred?[/quote]

Well, writing modular code can help you a lot, so I suspect that would be the preferred method, but ultimately it comes down to what makes the most sense to you, and what would be easier to maintain when you, or another developer comes back to this project in a couple of years time to add new features.

[quote author="tom756" date="1368299014"]I know when I've done something similar in a non-Codeigniter site I have just used specific php file to process the Ajax request. But I'm not sure in the context of Codeigniter what needs to be done in the context of the MVC framework.[/quote]

Controller receives the AJAX request.
Controller calls upon a model to insert the data into the database (if it's not already there).
Controller echoes a response back to the browser.

Simple. Smile

Hope this helps.
#3

[eluser]Unknown[/eluser]
Thanks TheFuzzy0ne!

For your detailed reply and clear explanation.

That answers my question.




Theme © iAndrew 2016 - Forum software by © MyBB