How to Pass Checkbox Value 0 If Not Checked and 1 If Checked Using Array Laravel

How to pass Checkbox value 0 if not checked and 1 if checked using array laravel

checkboxes are only posted when they are checked, so in controller you can use this fragment

public function updateMon(Request $request) {
$request->validate([
'account' => 'required|array',
'account.*' => 'integer'
]);


$myVar = isset($request->account[0]) ? 1 : 0;
}

How to pass Checkbox value 0 if not checked and 1 if checked using array

Use $request->has()

$technicien = new technicien();
$technicien->user_id = $user->id;
if($request->has('actif')){
$technicien->actif = $request->input('actif');
}else{
$technicien->actif = 0;
}
$technicien->moyenne_avis = $request->input('moyenne_avis');
$technicien->save();

How to handle unchecked checkboxes in Laravel?

From mozilla documentation on checkbox:

If a checkbox is unchecked when its form is submitted, there is no value submitted to the server to represent its unchecked state (e.g. value=unchecked); the value is not submitted to the server at all.

So, the following will do the trick:

$isChecked = $request->has('checkbox-name');

How to submit 0 if checkbox is unchecked and submit 1 if checkbox is checked in HTML

Simplest one, no javascript required, just put a hidden input before the checkbox:

<input type="hidden" name="check[0]" value="0" />
<input type="checkbox" name="check[0]" value="1" />

Inputs need to have the same name. If the checkbox is checked then value 1 will be submitted, otherwise value 0 from the hidden input.

Your case javascript solution, no hidden inputs needed:

<script type="text/javascript">
// when page is ready
$(document).ready(function() {
// on form submit
$("#form").on('submit', function() {
// to each unchecked checkbox
$(this + 'input[type=checkbox]:not(:checked)').each(function () {
// set value 0 and check it
$(this).attr('checked', true).val(0);
});
})
})
</script>

<form method="post" id="form">
<input type="checkbox" name="check[0]" value="1" />
<input type="checkbox" name="check[1]" value="1" />
<input type="submit" value="Save Changes" />
</form>

PHP solution, no hidden inputs needed:

<?php
// if data is posted, set value to 1, else to 0
$check_0 = isset($_POST['check'][0]) ? 1 : 0;
$check_1 = isset($_POST['check'][1]) ? 1 : 0;
?>

<form method="post">
<input type="checkbox" name="check[0]" value="1" />
<input type="checkbox" name="check[1]" value="1" />
<input type="submit" value="Save Changes" />
</form>

EDIT: the javascript solution is not valid anymore as of jquery 1.6. Based on this, a more proper solution is the following:

<script type="text/javascript">
// when page is ready
$(document).ready(function() {
// on form submit
$("#form").on('submit', function() {
// to each unchecked checkbox
$(this).find('input[type=checkbox]:not(:checked)').prop('checked', true).val(0);
})
})
</script>

<form method="post" id="form">
<input type="checkbox" name="check[0]" value="1" />
<input type="checkbox" name="check[1]" value="1" />
<input type="submit" value="Save Changes" />
</form>

Laravel: keep the checkbox selection after a validation error?

I think you are an invalid value pass in the in_array function

Try this:

value="delete-user" {{ (is_array(old('permissions')) && in_array('delete-user', old('permissions'))) ? ' checked' : '' }}> 

Laravel how to test if a checkbox is checked in a controller

I believe your real problem is that you have two separate forms. Your checkbox is in one form, your submit button is in a second form. I believe they both need to be in the same form. Otherwise your checkbox state is never returned, regardless of it's state.

In your view, try replacing the form markup you provided with this:

<form data-toggle="validator" data-disable="false" role="form" action="/admin/role/add" method="post">
<div class="checkbox checkbox-success">
<input name="test" id="test" type="checkbox" value="test">
<label for="test" style="padding-left: 15px!important;">test</label>
</div>
{{ csrf_field() }}
<div class="form-group pull-right">
<button type="submit" class="btn btn-danger">Submit</button>
</div>
</form>


Related Topics



Leave a reply



Submit