CodeIgniter Forums
Ajax form submit: Best way to handle post variables? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Ajax form submit: Best way to handle post variables? (/showthread.php?tid=26847)



Ajax form submit: Best way to handle post variables? - El Forum - 01-25-2010

[eluser]mhulse[/eluser]
Hi,

What is the best way to handle ajax post vars that are coming from an ajax form?

I built a view, for example:

site.com/class/function/param/param/param/

Here is where I am not sure how to proceed...

I have this form that submits to the above url:

site.com/class/function/

But what is the best way to pass the parameters? Should I just use $_POST[] like I would in standard (non-CI) PHP? Is there any CI code and/or standard approach to handling form submissions like this?

Is there any way I can utilize the code I wrote to handle the URI segments to also handle $_POST data? Kill two birds with one stone and minimal fuss type of thing?

My first thought was to use JS to change the form's action before submitting, but that just seems kinda ghetto.

Any tips would be very helpful. Smile

Thanks!
Micky


Ajax form submit: Best way to handle post variables? - El Forum - 01-25-2010

[eluser]Cro_Crx[/eluser]
CodeIgniter has an 'input' library which handles these sorts of things http://ellislab.com/codeigniter/user-guide/libraries/input.html. If you wanted to access $_POST['some_variable'] you'd use $this->input->post('some_variable');

CodeIgniters input class does some things automatically for you like check for cross(X) Site Scripting (XSS) attacks and some other stuff too.


Ajax form submit: Best way to handle post variables? - El Forum - 01-25-2010

[eluser]mhulse[/eluser]
Hi Cro_Crx! A billion thanks for the quick reply.

[quote author="Cro_Crx" date="1264424232"]CodeIgniter has an 'input' library which handles these sorts of things http://ellislab.com/codeigniter/user-guide/libraries/input.html. If you wanted to access $_POST['some_variable'] you'd use $this->input->post('some_variable');

CodeIgniters input class does some things automatically for you like check for cross(X) Site Scripting (XSS) attacks and some other stuff too.[/quote]

AHHHH, the "input" library! Cool!

Lol, I can't believe I missed that. Smile

Thanks Cro, I appreciate it. I promise I will read the manual a little more closely next time.

Also, thanks for the code example.

I am loving CI!

Cheers,
Micky


Ajax form submit: Best way to handle post variables? - El Forum - 01-25-2010

[eluser]mhulse[/eluser]
Just out of curiosity, is this how you might handle post data in a controller:

Code:
public function foo($bar = FALSE)
{
    $data = ($bar) ? $bar : $this->input->post('bar', TRUE);
    ...
    ...
}



Ajax form submit: Best way to handle post variables? - El Forum - 01-25-2010

[eluser]Cro_Crx[/eluser]
So if $bar is already set, use it. Otherwise use the POST data? Yeah that seems fine.


Ajax form submit: Best way to handle post variables? - El Forum - 01-25-2010

[eluser]mhulse[/eluser]
[quote author="Cro_Crx" date="1264426174"]So if $bar is already set, use it. Otherwise use the POST data? Yeah that seems fine.[/quote]

Cool! Yah, and if nothing exists, then I would have a boolean false to play around with. I just tested, and appears to work pretty smoothly.

Thanks again for your help! I really appreciate it. Smile

M