I want make function who can switch 2 values - Yes and No in Database. Now i have checkbox type who get me 1 value - "Yes" and when is not checked dont give me value. I want make in controller to switch when I dont get value to switch to "No" value. Now my function work, but change only last result, not this who i want. https://i.imgur.com/os12J8p.png & https://i.imgur.com/PR3K0lT.png. This is my code
<form method="post" action="/adminpanel/hofswitch">
#csrf
<div class="card-body">
#foreach($char as $value)
<div class="card-body">
#if($value->status == "Yes")
<input type="checkbox" name="switch[]" value="{{$value->id}}" checked data-bootstrap-switch data-off-color="danger" data-on-color="success">
<div class="form-group">
<label class="col-form-label" for="inputSuccess"><i class="fas fa-check"></i> Character Class</label>
<input type="text" name="class[]" value="{{$value->class}}" class="form-control is-valid" id="inputSuccess" readonly="true" placeholder="{{$value->class}}">
</div>
#else
<input type="checkbox" name="switch[]" value="{{$value->id}}" data-bootstrap-switch data-off-color="danger" data-on-color="success">
<div class="form-group">
<label class="col-form-label" for="inputError"><i class="far fa-times-circle"></i> Character Class</label>
<input type="text" name="class[]" value="{{$value->class}}" class="form-control is-invalid" id="inputError" readonly="true" placeholder="{{$value->class}}">
</div>
#endif
</div>
#endforeach
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary col-12">Submit</button>
</div>
</form>
and Controller
public function hof_switch(Request $request) {
$count = count(collect($request->get('switch')));
for($i = 0; $i < $count; $i++) {
$switch = $request->switch[$i] ?? null;
if ($switch == true) {
$switch = "Yes";
} else {
$switch = "No";
}
$update = DB::connection('XWEB')->table('XWEB_HOF')
->where('id', $request->switch[$i])
->update(
[
'status' => $switch,
]);
}
return redirect()->back()->withSuccess('You have switch this class successfully!');
}
Fews days ago I've been in similar problem
Put a hidden input with same name, and then try to POST it.
If the checkbox is checked, than the value will be send 1.
And If the checkbox isn't checked, than the value will be send only 0.
<form>
<input type="hidden" name="switch[]" value="0">
<input type="checkbox" name="switch[]" value="1">
</form>
Related
I'm trying to make student skip form and adding values to array, but there is "Undefined offset: 3" error.
Here is the controller:
public function skip(Request $request, $data = array())
{
$students = Student::where('group_id', 1)->get();
$result=[];
if ($request->has('submit')) {
foreach($students as $student) {
array_push($result, $data[$student->id]);
}}
return view ('skip', compact('students', 'result'));
}
}
And view:
<form method="POST" action="">
#csrf
#foreach($students as $student)
<div class="radiobtns-group">
<div class="btn-group btn-group-toggle" data-toggle="buttons">
{{$student->id}}
<label class="btn btn-success">
<input type="radio" name="{{$student->id}}" value="1" autocomplete="off" checked>Yes
</label>
<label class="btn btn-danger">
<input type="radio" name="{{$student->id}}" value="0" autocomplete="off">No
</label>
</div>
</div>
#endforeach
</div>
<button name='submit' type="submit" class="btn btn-primary">
Add
</button>
</form>
{{print_r($result)}}
I know, it needs to be done in JS, but I don't know it yet. Maybe you can help or guide in other ways to solve it.
Thank You!
You don't need $data, you need to get it from $request object.
And you need to specify your radio inputs name and value correctly like this.
I will show how to achieve it below.
#foreach($students as $key => $student)
<div class="radiobtns-group">
<div class="btn-group btn-group-toggle" data-toggle="buttons">
{{$student->id}}
<label class="btn btn-success">
<input type="radio" name="student-{{$key}}" value="1"
autocomplete="off" checked>Yes
</label>
<label class="btn btn-danger">
<input type="radio" name="student-{{$key}}" value="0"
autocomplete="off">No
</label>
</div>
</div>
#endforeach
And now you can receive student-$key(it can be like student-0, student-1 etc.) property in your $request option.
Now you can iterate throw your database students and do like below.
public function skip(Request $request)
{
$students = Student::where('group_id', 1)->get();
$result = [];
if ($request->has('submit')) {
foreach($students as $key => $student) {
if(!$request->has("student-$key")) continue; // In case when none answered
$result[$student->id] = $request->get("student-$key");
}
}
return view ('skip', compact('students', 'result'));
}
So we have collected $result which key is student id and value is 1 or 0.
I have a collection of checkboxes in my application. What I want to do is validate whether at least one is checked and enable a submit button. Basically a front end validation. According to the current code, the user must select all the checkboxes. I know this should be a small issue but as I am new to Laravel framework it's a little bit confusing for me to understand. Please, someone, help me here.
#foreach ($roles as $role)
<li>
{{ Form::checkbox('roles[]', $role->id, null, ['class="role-li"', 'required']) }}
{{ Form::label($role->name, ucfirst($role->name)) }}
</li>
#endforeach
<form id="testform" action="/comment" method="post" class="m-0 mt-4">
#for($i=0;$i<5;$i++)
<input type="checkbox" name="vehicle1" value="Bike" class="role-li role{{$i}}" onclick='checktest()'> I have a bike<br>
#endfor
</form>
<script>
var minimumonechecked;
var noofboxes = $('.role-li').length;
function checktest(xyz) {
minimumonechecked = false;
for(i=0;i<noofboxes;i++){
if ($('.role' + i).is(':checked')) {
minimumonechecked = true;
}
}
console.log(minimumonechecked)
};
</script>
this code works for me :)
Try this
var checkboxes = $(".check-cls"),
submitButt = $(".submit-btn");
checkboxes.click(function() {
submitButt.attr("disabled", !checkboxes.is(":checked"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Button should be enabled if at least one checkbox is checked</h1>
<form>
<div>
<input type="checkbox" name="roles[]" id="role-1" class="check-cls"> <label for="role-1">Role 1</label>
</div>
<div>
<input type="checkbox" name="roles[]" id="role-2" class="check-cls"> <label for="role-2">Role 2</label>
</div>
<div>
<input type="checkbox" name="roles[]" id="role-3" class="check-cls"> <label for="role-3">Role 3</label>
</div>
<div>
<input type="checkbox" name="roles[]" id="role-4" class="check-cls"> <label for="role-4">Role 4</label>
</div>
<div>
<input type="checkbox" name="roles[]" id="role-5" class="check-cls"> <label for="role-5">Role 5</label>
</div>
<div>
<input type="submit" class="submit-btn" value="Submit" disabled>
</div>
</form>
jsfiddle
I've created a function in my routes that take some data from a view and send to another view
Route::post('/trans', function(){
$j = Input::get('r');
return view('movs.create')->with($j);
});
this route take the data from this form
<form action="/trans" method="POST">
#csrf
<div class="input-group">
<input type="hidden" class="form-control" name="r" value={{$cooperado->id}}>
<button type="submit" class="btn btn-primary">
<span>+</span>
</button>
</span>
</div>
</form>
but cant set the data in this other form on 'movs.create'
<form method="post" action="{{ route('movs.store') }}">
<div class="form-group">
#csrf
<label for="name">ID COOP:</label>
<input type="number" class="form-control" name="id_coop" readonly/> <-- data must be setted here
</div>
<div class="form-group">
<label for="price">VALOR MOVIMENTACAO:</label>
<input type="number" step=0.01 class="form-control" name="valor"/>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
when i try to set the data in id_coop input, laravel says that the variable doesnt exists
To set data in the create form, you may need to add a value attribute to the id_coop input:
<input type="number" class="form-control" name="id_coop" value="{{ $j }} readonly/>
Also, ->with() needs to be a key (variable name) and value:
Route::post('/trans', function(){
$j = Input::get('r');
return view('movs.create')->with('id_coop', $j);
});
This would mean you use {{ $id_coop }} instead.
with works with key value pair
Route::post('/trans', function(){
$j = Input::get('r');
return view('movs.create')->with('j',$j);
// or return view('movs.create', compact('j')); // it will extract in
//blade as $j
// or return view('movs.create', ['j' => $j]);
});
// you can fetch that data in blade as {{$j}}
<input type="number" class="form-control" name="id_coop" value="{{$j ?? ''}}" readonly/>
Example of with,
return view('greeting')->with('name', 'Victoria'); // name as key and Victorial as value.
{{$j ?? ''}} if data is not set then '' value.
//controller
public function Postdata(Request $request){
$data['j'] = Input::get('r');
return view('movs.create',$data);
}
//route
Route::post('/trans','yourController#Postdata');
//your view
<form method="post" action="{{ url('/store') }}">
<div class="form-group">
#csrf
<label for="name">ID COOP:</label>
<input type="number" class="form-control" name="id_coop" value="{{ $j }}" readonly/> <-- data must be setted here
</div>
<div class="form-group">
<label for="price">VALOR MOVIMENTACAO:</label>
<input type="number" step=0.01 class="form-control" name="valor"/>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
//store route
Route::post('/store','yourController#Savedata');
hope this helps
In this project, i make something like online test. my question is how to get radio name with many different name in laravel ? i use id to different their name so but i cant get their name in controller.
this is my view
<form action="{{url('kumpulkan')}}" method="POST" class="responsive-form">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#if(Session::get('jurusan') == Multimedia)
<?php
$no = 1;
;?>
#foreach($soal_multimedia as $row)
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="wrapper-soal">
<div class="pertanyaan">
<span class="no-soal"><?php echo $no++ ;?>. </span> <span class="teks-soal">{{$row->soal}}</span>
</div>
<div class="jawaban">
<input type="number" readonly="readonly" name="id_soal" value="{{$row->id}}" hidden="hidden">
<div class="jawaban-item jawaban-a">
<input type="radio" name="jawaban_{{$row->id}}" value="{{$row->jawaban_a}}"> {{$row->jawaban_a}}
</div>
<div class="jawaban-item jawaban-b">
<input type="radio" name="jawaban_{{$row->id}}" value="{{$row->jawaban_b}}"> {{$row->jawaban_b}}
</div>
<div class="jawaban-item jawaban-c">
<input type="radio" name="jawaban_{{$row->id}}" value="{{$row->jawaban_c}}"> {{$row->jawaban_c}}
</div>
<div class="jawaban-item jawaban-d">
<input type="radio" name="jawaban_{{$row->id}}" value="{{$row->jawaban_d}}"> {{$row->jawaban_d}}
</div>
</div>
</div>
</div>
</div>
#endforeach
#endif
<button class="btn btn-default btn-lg">Selesai</button>
</form>
this is my controller
public function Postkumpulkan(Request $request)
{
$insert = array();
$insert['id_soal'] = $request->get('id_soal');
$insert['nama'] = Session::get('nama');
$insert['no_peserta'] = Session::get('no_peserta');
$insert['sekolah'] = Session::get('sekolah');
$insert['jurusan'] = Session::get('');
$insert['jawaban'] = $request->get('jawaban_{{$row->id}}');
}
i think you would better use checkbox and instead of this naming jawaban_{{$row->id}}
use this kind jawaban[{{$row->id}}] in this case you can get all jawabans in one array
like this :
$insert['jawaban'] = $request->get('jawaban');
Since I assume you intend to make your radio buttons to work as a radiogroup (yes, where you can only choose 1) and since that is how radio buttons work, you should just give the same name for all the radio buttons.
<div class="jawaban">
<input type="number" readonly="readonly" name="id_soal" value="{{$row->id}}" hidden="hidden">
<div class="jawaban-item jawaban-a">
<input type="radio" name="jawaban" value="{{$row->jawaban_a}}"> {{$row->jawaban_a}}
</div>
<div class="jawaban-item jawaban-b">
<input type="radio" name="jawaban" value="{{$row->jawaban_b}}"> {{$row->jawaban_b}}
</div>
<div class="jawaban-item jawaban-c">
<input type="radio" name="jawaban" value="{{$row->jawaban_c}}"> {{$row->jawaban_c}}
</div>
<div class="jawaban-item jawaban-d">
<input type="radio" name="jawaban" value="{{$row->jawaban_d}}"> {{$row->jawaban_d}}
</div>
</div>
Then you can just retrieve it in the controller:
$insert['jawaban'] = $request->get('jawaban');
It will retrieve the chosen radio button.
I'm building a task list where I can add and delete fields dynamically for each user. The problem I have, is saving the data from the checkbox.
I'm using the following code:
<form>
#foreach($tasks as $task)
<div class="col-md-9">
<input id="taskfield" name="dynamic[]" value="{{ $task->name }}" class="form-control field" type="text">
</div>
<div class="col-md-3">
<div class="slideOne">
<input type="checkbox" value="1" id="slideOne" name="check[]" #if($task->finished == 1) checked #endif/>
<label for="slideOne"></label>
</div>
</div>
#endforeach
/*The JS will add the new items here. JS example:*/
<div class="col-md-9">
<input id="taskfield" name="dynamic[]" value="{{ $task->name }}" class="form-control field" type="text">
</div>
<div class="col-md-3">
<div class="slideOne">
<input type="checkbox" value="1" id="slideOne" name="check[]" #if($task->finished == 1) checked #endif/>
<label for="slideOne"></label>
</div>
</div>
</form>
For saving it dynamically, I'm using this in my controller:
DB::table('tasks')->where('user_id', '=', $_POST['uid'])->where('week', '=', $_POST['week'])->where('year', '=', $_POST['year'])->delete();
if(isset($_POST['dynamic'])){
$takenvdweek = $_POST['dynamic'];
$finished = $_POST['check'];
var_dump($finished);
foreach($takenvdweek as $key => $value) {
DB::insert('insert into tasks (name, finished, user_id, week, year) values (?, ?, ?, ?, ?)', array($value, $finished[$key], $_POST['uid'], $_POST['week'], $_POST['year']));
}
}
So my Saving method requires always a value for the checkbox, 0 when unchecked and 1 when checked. I tried it with a hidden field with a 0, but it didn't work because the 0 was always in the POST array. Also when I click the a label, the first foreach label is changing, not the second where I click on. I tried to fix this with a class, but was no success.
Edit:
The foreach is for tasks that exists, but I can also add new items with a piece op JS, they are also inside the form, so I can't give them a foreach-id. I updated my question with the JS example.
Try to set the task id as index of dynamic and check
See below
#foreach($tasks as $task)
<div class="col-md-9">
<input id="taskfield" name="dynamic[$task->id]" value="{{ $task->name }}" class="form-control field" type="text">
</div>
<div class="col-md-3">
<div class="slideOne">
<input type="checkbox" value="1" id="slideOne" name="check[$task->id]" #if($task->finished == 1) checked #endif/>
<label for="slideOne"></label>
</div>
</div>
#endforeach
--Edit---
if task id is not available then you can set index like below
$index = 0;
#foreach($tasks as $task)
<div class="col-md-9">
<input id="taskfield" name="dynamic[$index]" value="{{ $task->name }}" class="form-control field" type="text">
</div>
<div class="col-md-3">
<div class="slideOne">
<input type="checkbox" value="1" id="slideOne" name="check[$index]" #if($task->finished == 1) checked #endif/>
<label for="slideOne"></label>
</div>
</div>
$index++;
#endforeach
This should do:
#foreach($clubs as $club)
<input type="checkbox"
id="{{$club->getCode()}}"
name="clubs[]"
value="{{$club->getId()}}" /> {{$club->getName()}}<br>
#endforeach