CodeIgniter Forums

Full Version: Really Need Help Understanding The Input Class for GET/POST Request
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
Hello CI community.

I've been struggling with this piece of code where I make an ajax call from the view to the controller to pass form data for validation and to save to session.

That part works. Not how I want it to, but it works.

The issue is that the ellislab documentation says that I should be able to access key values (whether get or post) by $this->input->post('key') or $this->input->get('key').

For the one that works, when I do a post request I have to loop through all of the keys.

View:

Code:
jQuery(function ($) {

// bind to the submit event of our form
    $("form#current").submit(function(event){

        var $form = $(this);

        var $inputs = $form.find("input, select, button, textarea");

        var serializedData = $form.serialize();

        $inputs.prop("disabled", true);

        $.ajax({
            url: "<?php echo base_url() . uri_string(); ?>",
            data: serializedData,
            type: "post",
            dataType: 'json'
        }).done(function(data){

             (do something with returned data)

        });

}

Controller:

Code:
if ($this->input->post()){

        foreach($this->input->post() as $key => $value)
        {

            if ($key == 'form_field_name'){

                    (do something)
            }
            else
            {
                (do something)
            }

         }

   }

But if I do $this->input->post('form_field_name'), I get nothing..(?)

The one that doesn't work is the get request for the ajax call.

View:

Code:
function previousForm(){

    $.get( "<?php echo base_url() . uri_string(); ?>", { name: "previousform" } )
        .done(function( data ) {
            alert( "Data Loaded: " + data.sendback );
    }, "json");
}

Controller:

Code:
if ($this->input->get('name'))
  {
       $return_data = json_encode(array("sendback"=>"the request is here"));
        echo $return_data;
        exit();
  }

And with the above get request, I get nothing returned and echoed.

It's very confusing. I appreciate any response.

El Forum

[eluser]Ckirk[/eluser]
In your example that doesn't work the following is invalid json:
Code:
{ name: "previousform" }

it should be:
Code:
{ "name": "previousform" }

Could that be your issue?

El Forum

[eluser]Unknown[/eluser]
Thanks for responding. That's not the issue though. If you think the way I have it written should work, then it's probably something else.

El Forum

[eluser]Rolly1971[/eluser]
the $this->input->post() is empty because you are sending the data using the get method. not post.

Code:
$('form_id_here').submit(function( event ) {
event.preventDefault();
$.ajax({
    type: "POST",
    url: <?php echo site_url('uri_string_here'); ?>,
    data: $('form_id_here').serialize(),
    success: function(data){
        // do something here
    }
});
});

the above is the basic method i use for ajax with CI. seems to work just fine.

check jquery's doc's at the link below for info on the $.post and $.ajax methods:

http://api.jquery.com/jQuery.post/