Welcome Guest, Not a member yet? Register   Sign In
Ajax form submit: Best way to handle post variables?
#1

[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
#2

[eluser]Cro_Crx[/eluser]
CodeIgniter has an 'input' library which handles these sorts of things http://ellislab.com/codeigniter/user-gui...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.
#3

[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-gui...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
#4

[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);
    ...
    ...
}
#5

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

[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




Theme © iAndrew 2016 - Forum software by © MyBB