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.
}
}
data: { arr: chkBoxArray },.