0

I have a form that has an action dropdown and a series of checkboxs that have ids, i want to grab all the checkboxes that are checked, and the action chosen and send them via ajax to another page. current attempt is an array, what else can i try?

<select name='action' id='action'>
    <option value=''></option>
    <option value='reassign'>Reassign</option>
    <option value='merge'>Merge</option>
    <option value='move'>Move</option>
</select>

$('select').on('change',$('#action'),function(){
    data = [];
    $.each($('input:checkbox'),function(e,i){
        if($(this).is(':checked')){
            data[e] = $(this).attr('id');
        }
    });
    data.add = $(this).val();
    $.ajax({
        type:"POST",
        data:{data:data},
        url:"bulkChange.php",
        success: function(result){
            alert(result);
        }
    });
});

Currently, I do not see the action added to the array. print_r($_POST) on bulkChange.php shows:

[data] => Array
    (
        [0] => c_32481
        [1] => c_32477
        [2] => 
        [3] => 
        [4] => 
        [5] => c_32308
    )

i would like to see:

[data] => Array
    (
        [0] => c_32481
        [1] => c_32477
        [2] => 
        [3] => 
        [4] => 
        [5] => c_32308
        [6] => merge
    )
4
  • Might be irrelevant, but where is the i you have in your each handler function being used? Commented Mar 24, 2015 at 17:47
  • remaining from previous attempts. it can be removed. Commented Mar 24, 2015 at 17:48
  • Can include input html ? from $('input:checkbox') ? Commented Mar 24, 2015 at 17:52
  • @WashingtonGuedes for code cleaniness? Commented Mar 24, 2015 at 17:57

3 Answers 3

1

In your code, instead of using

data.add = $(this).val();

use data.push($(this).val());

That should do.

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

Comments

0
$('select').on('change',$('#action'),function(){
                    data = [];
                    $.each($(this).find('option'),function(e,i){
                            if($(this).is(':checked')){
                            data[e] = $(this).attr('value');
                            }
                    });
                   console.log(data);
                    });

Does this construct the data array as you'd like it to?

Comments

0

Try this instead of using

data.add = $(this).val();

$('select').on('change',$('#action'),function(){
    data = [];
    $.each($('input:checkbox'),function(e,i){
        if($(this).is(':checked')){
            data[e] = $(this).attr('id');
        }
    });
    //array push
    data.push($(this).val());
  
    $.ajax({
        type:"POST",
        data:{data:data},
        url:"bulkChange.php",
        success: function(result){
            alert(result);
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<select name='action' id='action'>
    <option value=''></option>
    <option value='reassign'>Reassign</option>
    <option value='merge'>Merge</option>
    <option value='move'>Move</option>
</select>

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.