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?

2
  • provide pls full html code Commented Jun 30, 2016 at 5:23
  • @СеменихинМаксим, This is all my full. the rest is just around 50 to 60 input checkbox. Nothing else. Commented Jun 30, 2016 at 6:30

2 Answers 2

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>
1
  • Actually is works. It been so many year never touch checkbox. I forget it will not post if never checked.....lol. Commented Jun 30, 2016 at 6:33
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()
    }
});
4
  • 1
    Give us some descriptions, why not type but method? Commented Jun 30, 2016 at 4:42
  • api.jquery.com/jquery.ajax 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. Commented Jun 30, 2016 at 5:22
  • Thanks for pointing that out @СеменихинМаксим - updated answer
    – zanderwar
    Commented Jun 30, 2016 at 5:30
  • @СеменихинМаксим, my js is {js('jquery-1.10.min.js',$attr,TRUE)} Commented Jun 30, 2016 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.