0

codeigniter 2.0

form.tpl:

<form class="form-book" id="class-form" method="POST">
  inside full of checkbox....
<button type="button" class="btn" id="submit-btn">Submit</button>
</form>
<script>
$(document).ready(function() {
    $('#submit-btn').click(function(){  
        $('#response').html("<b>Loading response...</b>");
                var serializedData = $form.serialize();
        $.ajax({
            type: 'POST',
            url: 'http://www.example.com/booking/checkbooking', 
            data: serializedData,
        })
        .done(function(data){
            $('#response').html(data);          
        })
        .fail(function() {
            alert( "Posting failed." );

        });
        return false;
    }); 
});
</script>

http://www.example.com/booking/checkbooking:

public function checkbooking(){
    print_r($_POST);
}

I cant get any post value. What have I miss?

zanderwar
  • 3,440
  • 3
  • 28
  • 46

2 Answers2

1

You should to use method: 'POST' not type: 'POST' if you're using a jQuery version larger than 1.9.0

If $_POST is empty, you're either not POSTING it, or it's being emptied and handled elsewhere for security.

You could also check the HTTP Headers and ensure the POST data is actually being sent, click here for instructions

type (default: 'GET') Type: String An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0.

An alias for method.

$.ajax({
    method: 'POST',
    url: 'http://www.example.com/booking/checkbooking', 
    data: $('form#class-form').serialize(),
    beforeSend: function() {
            // stuff to do before ajax request is made
    },
    success: function (data) {
            // same as .done()
    },
    error: function() {
            // same as .fail()
    }
});
Community
  • 1
  • 1
zanderwar
  • 3,440
  • 3
  • 28
  • 46
1

there is no element like $form, use $('#class-form') instead of $form.

<form class="form-book" id="class-form" method="POST">
  inside full of checkbox....
<button type="button" class="btn" id="submit-btn">Submit</button>
</form>
<script>
$(document).ready(function() {
    $('#submit-btn').click(function(){  
        $('#response').html("<b>Loading response...</b>");
                var serializedData = $('#class-form').serialize();
        $.ajax({
            method: 'POST',
            url: 'http://www.example.com/booking/checkbooking', 
            data: serializedData,
        })
        .done(function(data){
            $('#response').html(data);          
        })
        .fail(function() {
            alert( "Posting failed." );

        });
        return false;
    }); 
});
</script>
Arvind
  • 1,006
  • 10
  • 16