Hi,
It seems your selected variable is empty.
You should be able to post your form like this:
var data = $('#myForm').serializeArray();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello , i have an array of data which am trying to pass to a laravel function in controller using ajax. Basically the array contains a list of selected checkbox and their values.
here is my javascript and ajax code
<script type="text/javascript">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
document.getElementById('register_unit').addEventListener('click', function(){
var selected = new Array();
$(document).ready(function() {
$("input:checkbox[name=check]:checked").each(function() {
selected.push($(this).val());
});
console.log(postData)
$.ajax({
type: "POST",
url: '/unit/register',
data: selected,
cache: false,
success: function(data){
alert(data);
}
});
});
});
</script>
This is how am logging the data in my controller
public function saveUnits(Request $request)
{
\Log::info($request->all());
}
Upon logging , i receive an empty array in my log
[2019-03-14 12:23:27] local.INFO: array (
'undefined' => NULL,
)
Someone help!
Two things:
For things like this, you don't need to use new Array(), instead use var selected = [];
data should be an object, so try this instead: data: { selected: selected },
Please or to participate in this conversation.