不显示数组计数

I have number of checkboxes on my page and I want to get those checkbox values in my database.

$('#assign_data').on("click",function()
    {
        var mandate_array = [];
        var i= 0;

        $('.assign_mandate:checked').each(function(){
             mandate_array[i++] = $(this).val();
        });


        $.ajax
            ({
                type: "POST",   
                url: BASE_URL+"mandate/assign_mandate.php", 
                data: "mandate_array="+mandate_array+"&role_id="+$('#role').val()+"&user_id="+$('#user').val(), 
                success: function(msg)
                {
                    console.log(msg)

                }
            })

    });

assign_mandate.php :

<?php

  $mandate = explode(',',$_POST['mandate_array']);

  // print_r($mandate);  //it shows the data in array

  count($mandate);exit;  // it does not show the count in console
?>

When I print the array it show me the array data in console but when I try to echo the count of array it shows blank. Why ?

Thanks in advance

You have to echo the variable count value.

    <?php

  $mandate = explode(',',$_POST['mandate_array']);

  // print_r($mandate);  //it shows the data in array

  echo count($mandate);exit;  // it does not show the count in console
?>

Use echo in front of the count()

Use JSON.stringify.

You would typically convert the array to a JSON string with JSON.stringify, then make an AJAX request to the server, receive the string parameter and json_decode it to get back an array in the PHP side.

$('#assign_data').on("click",function()
    {
        var mandate_array = [];
        var i= 0;

        $('.assign_mandate:checked').each(function(){
             mandate_array[i++] = $(this).val();
        });


        $.ajax
            ({
                type: "POST",   
                url: BASE_URL+"mandate/assign_mandate.php", 
                data: "mandate_array="+JSON.stringify(mandate_array)+"&role_id="+$('#role').val()+"&user_id="+$('#user').val(), 
                success: function(msg)
                {
                    console.log(msg)

                }
            })

    });

mandate_array is Array so you wont posted array data in query string, you should use JSON.stringfy() function to convert array/JSON object into string.

   $.ajax
        ({
            type: "POST",   
            url: BASE_URL+"mandate/assign_mandate.php", 
            data: "mandate_array="+JSON.stringfy(mandate_array)+"&role_id="+$('#role').val()+"&user_id="+$('#user').val(), 
            success: function(msg)
            {
                console.log(msg)

            }
        })

In PHP code

var_dump(json_decode($_POST['mandate_array']));