1

I have CodeIgniter question. How can I pass an array from view to controller? I am not able to send data to controller from view by calling public function sms() on a button click.

Here is my code that doesn't work:

<script>
function send_sms() {
    var chkBoxArray = new Array();
        $(document).ready(function (e) {

        $('#table input[type="checkbox"]:checked').each(function () {
            var getRow = $(this).parents('tr');
            chkBoxArray.push(getRow.find('td:eq(9)').html());
        });

        alert(chkBoxArray);
        reload_table();
    });

    $.ajax({
        url: "<?php echo site_url('person/sms')?>",
        type: "POST",
        data: { 'arr': chkBoxArray },
        dataType: "JSON",
        success: function (data) {
            // if success reload ajax table
            // alert(chkBoxArray);
            // reload_table();
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert('Error adding / update data');
        }
    });
}
</script>

Controller code :

public function sms() {
  $arr = $this->post('arr');
  foreach($arr as $ar) {
    echo $ar; // prints each element of the array.
  }
}
2
  • 1
    Not clear what specific problem is. "doesn't work" is a meaningless problem statement and doesn't suggest what part isn't working Commented Mar 7, 2016 at 14:42
  • Try first with data: { arr: chkBoxArray },. Commented Mar 7, 2016 at 21:25

1 Answer 1

0

You should use : $arr = $this->input->post('arr'); to retrieve post values, and if you expecting the returning result is a JSON type, then in server side you need to tweak the PHP code to return it by using echo json_encode($arrayVariable);, otherwise just omit dataType:json at AJAX properties.

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

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.