learnallskill learnallskill - 5 months ago 24
Ajax Question

Ajax Post - Unable to get Post variable

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?

Answer

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({
            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>