3

Problem solved

I've searched several sources of info for this kinda problems. But there's no solution exactly. The problem is I cannot pass the checkboxes value to div id with ajax. It keeps openning in a parent window. Here's my codes.

Javascript:

<script type="text/javascript">
$(document).ready(function() {  
    $("#villa_submit").click(function() {   
        var action = $("#villa_choose").attr('action');
//var form_data = { 'vid[]' : []};$("input:checked").each(function() {data['vid[]'].push($(this).val());});
var form_data = $('#villa_choose').serialize();  //suggested by Kev Price 
        $.ajax({
            type: "POST",
            url: action,
            data: form_data,
            beforeSend:function(){
                $('#villa_result').html('<center><img src="/images/loading.gif" alt="Loading..." align="absmiddle"/> loading...</center>');
              },
            success: function(data){
                  $('#test_result').html(data);
            }
        });     
        return false;
    }); 
});
</script>

HTML:

<form name="villa_choose" id="villa_choose" method="post" action="">
<input type="checkbox" name="vid[]" id="vid1" value="1" />
<input type="checkbox" name="vid[]" id="vid2" value="2" />
<input type="checkbox" name="vid[]" id="vid3" value="3" />
<input type="checkbox" name="vid[]" id="vid4" value="4" />
<input type="checkbox" name="vid[]" id="vid5" value="5" />
</form>

<div id="test_result"></div>
3
  • 6
    have you tried sending the form data as a serialised array? i.e. var form_data = $('#villa_choose').serialize(); Commented Nov 8, 2012 at 17:18
  • @KevPrice, Your suggestion is work! Thank you. Commented Nov 8, 2012 at 17:32
  • 1
    Glad to hear it - I probably should have added it as a solution rather than a comment. Probably worth editing your original question to show the solution so that it is obvious Commented Nov 8, 2012 at 17:39

3 Answers 3

3

You have multiple <input> elements with this same id attribute. This is not allowed, id must be unique in the document. Fix that and try again.

Sign up to request clarification or add additional context in comments.

1 Comment

Downvoter, please indicate your reason for doing so.
3

Once you fix the issues Madbreaks suggested you can get the check box values into an array like this in your jquery

var values = $("input[type='checkbox']:checked").map(function () {
    return this.value;
}).get();

values will be an array that you can pass with ajax like this

var data = 'checkbox="'+values+'"';
$.ajax({
    url: "process.php",
    type: "GET",
    data: data,
    cache: false,
    success: function (html) {
        ... // change html on return
    }
});

Hope that helps

1 Comment

Thank you for your attempt. I found a solution. :)
0

var data = $("#villa_choose").serializeArray();

$.ajax({

url: "process.php",

type: "GET",

data: data,

cache: false,

success: function (html) {

})

});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.