Welcome Guest, Not a member yet? Register   Sign In
Best practise for $_POST and Ajax CRUD
#1

[eluser]CARP[/eluser]
Hi guys

1) I've a form, inside a view, and I'd like if my logic is going the best and quickest way possible

some_controller.php
Code:
if ($this->input->post('some_field')) {
    $this->load->model('model_person');
    $this->model_person->add($field1, $field2, $field3, ...);
}
else {
    $this->load->view('form_add_person');
}

What if my form has 25 fields? Could I send only the $_POST array through the model function's parameter? or is there a better way?

2) I want to reduce files, views, controllers. Did any1 try an easy way to do ajax with CI? I've been able to delete a record using jQuery ajax load function, and would like to know if there're other (easier) ways to try

Thanks a lot in advance,
#2

[eluser]Seppo[/eluser]
1) you could... although it is not good practice... the best way (or at least, the one I prefer) is to send an array with the parameters, something like
Code:
$this>model_person->add(array(
    'field1' => $this->input->post('field1'),
    'field2' => $this->input->post('field2'),
    'field3' => $this->input->post('field3'),
    'field4' => $this->input->post('field4'),
// and so on...
));
#3

[eluser]CARP[/eluser]
thanks Seppo
knowing you're awake on saturday early Big Grin, lemme ask: do you use Ajax with CI? for doing CRUD stuff?
#4

[eluser]Seppo[/eluser]
I have a test on monday, but I can't study so... =)

I do use Ajax with CI... not much, but I do... usually I like to not break any functionality when the user does not use or allow JS, so I first do it without any javascript, all the cycle, and then add JS to submit the form and stuff... There I add a post var named for example "ajax" set to "1", and I switch the server response, instead of showing a full page, it only shows the piece of html I need
#5

[eluser]CARP[/eluser]
cool...
I'm just looking for the best way to shorten the development process of creating crud for all the objects this CI app handles
#6

[eluser]Randy Casburn[/eluser]
@CARP -- I use AJAX extensively with CI. As you suspect, the application flow is different than using PHP to build the view for you. Since I use JSON to provide the output of the CRUD functions, I don't use views at all when using AJAX. In fact, I use a completely different set of Models for my AJAX responses than I do for classic CI responses. I actually thread an encrypted key with my AJAX requests for security and validate that key in my AJAX models. If you don't have the key, you can't invoke my AJAX methods. Chances of getting the correct key are greatly diminished unless you requested the correct page, etc. etc. As you know, AJAX security is very important and that is what makes CI an ideal platform for AJAX!

Normal flow -> TEST if AJAX -> IF NOT AJAX NORMAL CI
Code:
[REQUEST] -> [Controller] \             [no]/[Model] -> [View] -> [Output] ->
                           \               /
                            ? HttpRequest ?  
                                           \
                                       [yes]\
                                             [ajax/Model] -> [Output] ->
Normal flow -> TEST if AJAX -> IF AJAX Implement AJAX MODEL & echo json_encode()'d string directly to the browser.

That's it. This is efficient, and has worked flawlessly for me for a very long time.

My file structure is:

Code:
[root]
-assets
-- scripts (js goes here)
-- images
-- css
-system
-- application
    ---models
       ----ajax
           crud.php (maybe your ajax class here)

The beauty of this is that you can still move the system directory outside the document root for security purposes (this is a very good practice). Your AJAX will still work because CI is managing every aspect of finding and executing the correct model for you. The only files that must be in the root are those in the assets folder.

I hope you find this helpful.

Randy




Theme © iAndrew 2016 - Forum software by © MyBB