Sarah
Sarah

Reputation: 459

Why is $_POST empty in Codeigniter?

I've seen questions similar to this one, but still didn't find the solution, and this is really wasting my time :(

Here's my controller

public function test() {
   print_r($_POST);
   return;
}

View

<?php 
    echo form_open(base_url('go/test'), 'id="update" autocomplete="off" ajax-hidden'); 
    echo form_hidden('id', $id); 
?>
    <input id="update" name="number" class="spinner" readonly="readonly" 
       value="<?$=value?>"/>
<?php 
    echo form_close();
?>

I'm using jQuery to submit the form once the jQuery UI spinner is changed. This is the response:

Array()

Upvotes: 0

Views: 2918

Answers (4)

Sarah
Sarah

Reputation: 459

I forgot that I changed my ajax handler to work with IE and didn't change the ajax function for this particular form, because this form's response should be hidden, so it uses a different ajax handler.

This is my main submit function:

(function($){
    jQuery.fn.ajaxFormSubmit =
        function(container, data) {
            var url = $(this).attr('action');
            $.ajax({
                    url: url,
                    type: "POST",
                    data: data,
                    dataType: "html",
                    success: function(msg) {
                                $(container).html(msg);
                        }
                   });
            return this;
         };
})(jQuery);

I used to serialize the form data inside this function but then I realized it didn't work for IE, the only solution was to serialize the form data before doing anything else!

Here are the 2 functions I'm using:

$('form[ajax]').live('submit', function(e){
    e.preventDefault();
    var data = $(this).serialize();
    var container = $(this).attr('cont');
    if (typeof container == 'undefined' || container == false) {
        container = '.#ajax';
    }
    $(container).html('<img src="public/images/ajax.gif"/>');
    $(container).fadeIn('fast');
    $(this).ajaxFormSubmit(container, data);
});

$('form[ajax-hidden]').live('submit', function(e){
    e.preventDefault();
    var data = $(this).serialize();
    var container = '';
    $(this).ajaxFormSubmit(container, data);
});

The second function simply didn't have the data variable. So I added it. Hope this can help someone.

Upvotes: 1

Michael Sole
Michael Sole

Reputation: 124

Also you should use codeingiters post handlers $something = $this->input->post('something');

http://ellislab.com/codeigniter/user-guide/libraries/input.html

Upvotes: 2

Kylie
Kylie

Reputation: 11759

Change your input id to something other than "update" And change < ?$=value > to < ?=$value >

<input id="myInput" name="number" class="spinner" readonly="readonly" value="<?= $value?>"/>

Upvotes: 1

sikander
sikander

Reputation: 2286

Most likely jQuery is not able to get a value to pass over to your Controller because your form and input elements have the same id update

Also <?$=value?> is incorrect syntax. It should be <?= $value ?>

Upvotes: 3

Related Questions