CodeIgniter Forums

Full Version: Best way to handle forms
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
I'm working on a community website with members.
Now one of the first things I need to decide is how to work with forms in CodeIgniter.

Option 1
=======
In my view file I use a form like this:
<?php echo form_open('form'); ?>

<h5>Username</h5>
&lt;input type="text" name="username" value="" size="50" /&gt;

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;

Then I have a controller that handles the form and, if necessary, I use a model to insert data for example.
The advantage is that it's simple and not much work is needed. I can even generate my own form using a website like http://formigniter.org/app/.
Big problem for me is that this method always reloads the view page and I don't want to refresh a page.

Option 2
=======
The other option is to use jQuery/Ajax.

In my view I have a simple input field:
&lt;input type="text" id="username" name="username" size="50" /&gt;

And I have some javascript code:
$('#username').change(function() {

var post_data = {
'message': $("#username").val()
};

$.ajax({
type: "POST",
url: "&lt;?php echo base_url(); ?&gt;index.php/registratie_c/insertByajax",
data: post_data,
success: function(message) {
alert(message);
}
});
});

In my controller I need to get the data like this:
$data['message'] = $this->input->post('message');

After that I can do whatever I want.
Big advantage is that I don't have to reload my page, as a user types the data is being sent.
My problem here is that it's much more work to. I have to write some javascript to get the data from the different input fields. In my controller I have to do this another time and there's no form generator.

My issue/question is, which one do you prefer?
And isn't there a solution that's simple but also let's you post data on the fly? The above 2 options still feel pretty undeveloped.

p.s. I do know there's an option to post a whole form using serialize...