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.
Related
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>
I'm trying to display the options for the questions. i have the problem that when im doing it this way:
#section('content')
<div class="card">
<div class="card-header">Quiz: {{$category->name}}:</div>
<div class="card-body">
<form action="#" method="post" class="form-group">
#csrf
#foreach($questions as $key => $question)
<div class="form-group">
<label for="question">Question {{1+$key}}:</label>
<p class="card-text">{{$question->question_text}}</p>
#foreach($question->option_text as $key => $option)
<input type="radio">{{$option}}
#endforeach
</div>
#endforeach
<div class="form-group">
<input type="submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
i can check all radio buttons at the same time but if i put there a name for the radiobutton i can check only one of the options for the whole questions.. i think there's something strange with the foreach loop..
Info: the table "questions" has the following rows:
id, category_id, question_text, correct_answer, option_text (this is a json-field that is casted to an array)
Code from Controller:
public function store(Request $request, Category $category){
if($request->categoryTest == $category->name){
$questions = $category->question()->inRandomOrder()->get();
return view('user.test', compact('questions', 'category'));
}
}
do you have an idea how to fix this? thank you!
Try this:
#section('content')
<div class="card">
<div class="card-header">Quiz: {{$category->name}}:</div>
<div class="card-body">
<form action="#" method="post" class="form-group">
#csrf
#foreach($questions as $key => $question)
#php
$q = 1+$key
#endphp
<div class="form-group">
<label for="question">Question {{1+$key}}:</label>
<p class="card-text">{{$question->question_text}}</p>
#foreach($question->option_text as $key => $option)
<input name="radio-{{$q}}" type="radio">{{$option}}
#endforeach
</div>
#endforeach
<div class="form-group">
<input type="submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
This seems to be a normal behavior of the radio buttons, only one is market at time. Have you tried to use checkbox instead?
Also, if you want to send it to backend, you'll need to set a name attribute. And here is the magic: instead of using a singular name, put it as array in order to get an array on your controller.
<input type="checkbox" name="question[]" class="btn btn-primary">
I am new to laravel. I have two tables in my database. I want the data to be shown from the database in my select drop down. But when I pass the data to the view file it gives an error that the undefined variable $groups. Please help me. Here is my view form.
<form method="post" id="frm">
{{csrf_field()}}
<div class="panel-body">
<div class="row text-center">
<div class="col">Group
<select class="selectpicker boundary" data-live-search="true">
#foreach($group as $i)
<option value="{{$i->id}}">{{$i->gtitle}}</option>
</select>
</div>
</div><br>
<div class="row text-center">
<div class="col title">Title
<input type="text" name="title" id="title">
</div>
</div>
</div>
<div class="panel-footer text-center">
<input type="submit" name="submit" id="submit" value="Submit" class="btn btn-info"/>
<input type="reset" name="reset" value="Reset" class="btn btn-secondary"/>
</div>
</form>
this is my controller
public function index(){
$group = Group::all();
return View::make("index")->with(array('groups'=>$groups));
}
this is my route
Route::get('/index', 'GroupController#index');
this is my model
class Group extends Model{
protected $table = 'groups';
protected $fillable = ['gtitle'];
}
This should work, worked very well on my code.
public function index(){
$group = Group::all();
return view('index') // location of index file
->with('group', $group);
}
<select class="selectpicker boundary" data-live-search="true">
#foreach($group as $i)
<option value="{{$i->id}}">{{$i->gtitle}}</option>
</select>
use compact instead of an array
public function index(){
$group = Group::all();
return View::make("index",compact('group'));
}
I'm having a problem with a CRUD application form being displayed on a bootstrap modal.
The issue happens when the form's edit button send the "editId" post var, the modal shows up but the values for the options of the select tag are displayed outside the form.
My controller looks like this:
public function editarUsuarioController(){
if (isset($_GET["editId"])) {
$dataController = $_GET["editId"];
$data = Datos::editUserModel($dataController, "users");
echo'<div id="editModal">
<form method="post" role="form">
<div class="form-group">
<label for="roleEdit">Rol<span></span></label>
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-user"></i>
</span>
<select name="roleTypes" class="form-control">
<option selected>
'.$data["rols"].'
</option>
'.$editOptions = MainController::viewRolesController().'
</select>
</div>
</div>
<div class="form-actions">
<input type="submit" class="btn blue" value="Update">
<button type="button" data-dismiss="modal" class="btn default">Cancel</button>
</div>
</form>
</div>';
} }
As you can see, I'm instantiating another controller from another file which lists the available roles which looks like:
public function viewRolesController(){
$response= Datos::vistaRolesModel("roles");
foreach($response as $row => $item){
echo'<option value="'.$item["id"].'">'.$item["role"].'</option>';
}
}
Any suggestions?
I fixed it by splitting up the echo and using a variable inside the original controller to store the values brought by the vistaRolesModel() model function so this is the new editarUsuarioController() controller function:
public function editarUsuarioController(){
if (isset($_GET["editId"])) {
$dataController = $_GET["editId"];
$data = Datos::editUserModel($dataController, "users");
$roles = Datos::vistaRolesModel("roles");
echo'
<div id="editModal">
<form method="post" role="form">
<div class="form-group">
<label for="roleEdit">Rol<span></span></label>
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-user"></i>
</span>
<select name="roleTypes" class="form-control">
<option selected>
'.$data["rols"].'
</option>
';
foreach($roles as $row => $item){
echo'<option value="'.$item["id"].'">'.$item["role"].'</option>';
}
echo'
</select>
</div>
</div>
<div class="form-actions">
<input type="submit" class="btn blue" value="Update">
<button type="button" data-dismiss="modal" class="btn default">Cancel</button>
</div>
</form>
</div>
';
}
}
I'd appreciate your help with this!
Here is my bootstrap HTML code:
<div class="form-group centered">
<input type="submit" class="btn btn-primary btn-block" name="login" value="Log In"></input>
</div>
<div class="centered">
<div class="form-group btn-group" data-toggle="buttons">
<label class="btn btn-default active">
<input type="radio" id="app" value="app" name="dashboard"/>Application
</label>
<label class="btn btn-default">
<input type="radio" id="setup" value="setup" name="dashboard"/>Setup
</label>
</div>
</div>
I would like to add some logic that after the "Log In" button is selected, the "active" class on the label is retained properly.
Example:
Setup is selected
The user clicks login
The password is wrong so the user is thrown back to the same log in form
This time the form has the setup button "active" automatically
I tried playing around with the following within the app input type:
<?php if($_POST['dashboard'] == "app") { echo checked="\"checked\""; } ?>
I now realize, however, that this button group does not work like a regular radio button, and I probably need to toggle the class on the labels somehow.
Thanks in advance for your help!
I got it working piggybacking on the advice of user1844933 :)
<div class="form-group centered">
<input type="submit" class="btn btn-primary btn-block" name="login" value="Log In"></input>
</div>
<div class="centered">
<div class="form-group btn-group" data-toggle="buttons">
<label class="btn btn-default <?php setAppDefault( $lastDashboardSetting ); ?>">
<input type="radio" value="app" name="dashboard"/>Application
</label>
<label class="btn btn-default <?php setSetupDefault( $lastDashboardSetting ); ?>">
<input type="radio" value="setup" name="dashboard"/>Setup
</label>
</div>
</div>
My PHP code:
$lastDashboardSetting = $_POST['dashboard'];
function setAppDefault( $setting ) {
if( $setting === "app" )
echo "active";
else
echo "";
}
function setSetupDefault( $setting ) {
if( $setting === "setup" )
echo "active";
else
echo "";
}
try this
php
if($_POST['dashboard'] == "app" { $appchk='checked'; } else {$appchk='';}
if($_POST['dashboard'] == "setup" { $setupchk='checked'; } else {$setupchk='';}
HTML
<input type="radio" id="app" value="app" name="dashboard" <?php echo($appchk);?>/>Application
<input type="radio" id="setup" value="setup" name="dashboard" <?php echo($setupchk);?> />Setup