I’m trying to save the value of the check box in laravel. I can save all the text fields and all the code works. When I try to save the value of the checkbox the code breaks. Here is what I’ve tried. All the help is appreciated.
Here is my controller I’ve
JavaScript
x
12
12
1
public function addTodo(Request $request)
2
{
3
$status = Todo::has('status') ? true: false;
4
5
$todo = new Todo;
6
$todo->item = $request->item;
7
$todo->description = $request->description;
8
$todo->status = $request->status;
9
$todo->save();
10
return response()->json($todo);
11
}
12
Here is my HTML
JavaScript
1
8
1
<div class="form-group">
2
<div class="col-sm-10">
3
<input type="checkbox" name="status" id="status" value="1"
4
placeholder="Your description Here" required>
5
</div>
6
</div>
7
</div>
8
Here is my script
JavaScript
1
46
46
1
$(document).on('click','.create-modal', function() {
2
$('#create').modal('show');
3
$('.form-horizontal').show();
4
$('.modal-title').text('Add Item');
5
});
6
7
$("#add").click(function() {
8
$.ajax({
9
type: 'POST',
10
url: '/addTodo',
11
data: {
12
'_token': $('input[name=_token]').val(),
13
'item': $('input[name=item]').val(),
14
'status':$('input[name="status[]:checked'].val()
15
},
16
success: function(data){
17
if ((data.errors)) {
18
$('.error').removeClass('hidden');
19
$('.error').text(data.errors.item);
20
21
} else {
22
$('.error').remove();
23
$('#table').append("<tr class='post" + data.id + "'>"+
24
"<td>" + data.id + "</td>"+
25
"<td>" + data.item + "</td>"+
26
"<td>" + data.status + "</td>"+
27
"<td><button class='show-modal btn btn-info btn-sm' data-id='" + data.id +
28
"' data-item='" + data.item + "' data-status='" + data.status + "'> +
29
<span class='fa fa-eye'></span></button> +
30
<button class='edit-modal btn btn-warning btn-sm' data-id='" +
31
data.id + "' data-item='" + data.item + "' data-status='" +
32
data.status + "'><span class='glyphicon glyphicon-pencil'></span></button> +
33
<button class='delete-modal btn btn-danger btn-sm' data-id='" +
34
data.id + "' data-item='" + data.item + "' data-status='" + data.status +
35
"'><span class='glyphicon glyphicon-trash'></span></button> +
36
</td>"+ "</tr>");
37
}
38
},
39
});
40
41
$('#item').val('');
42
$('#status').val('');
43
location.reload();
44
45
});
46
Advertisement
Answer
try this
JavaScript
1
2
1
$('input[name="status"]:checked').val()
2