It looks like you need to look into blades case and switch statements . https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwit6f6Ezdz2AhW1oFwKHZ0VBv0QFnoECAkQAQ&url=https%3A%2F%2Flaravel.com%2Fdocs%2F9.x%2Fblade&usg=AOvVaw0oJTUqZyG3T8U6nCOXYYTP
Dynamically set input value based on previous input selection
I am attempting to get a value from a previous input value and have it selected and possibly as a hidden field which I will use in a subsequent input. Not sure how to go about it. In essence, I have a student name input, and once you select the student, the next input's value should show that student's level. As of now, I can show all the student's levels in that input dropdown but just want to have that student's level selected. Here are my inputs so far:
<div class="col-md-3">
<div class="form-group">
<h5>Student Name <span class="text-danger">*</span></h5>
<div class="controls">
<select name="student_id[]" required class="form-control student-name" id="student_name">
<option value="" selected disabled>Select Student Name</option>
@foreach($students as $student)
<option value="{{ $student->id }}">{{ $student->name }}</option>
@endforeach
</select>
</div>
</div>
{{-- End form Group --}}
</div>
{{-- End Col md 3 --}}
<div class="col-md-3">
<div class="form-group">
<h5>Student Level <span class="text-danger">*</span></h5>
<div class="controls">
<select name="level_id[]" id="level_id" class="form-control" >
@foreach($students as $student)
<option value="{{ $student->level_id }}">{{ $student->level_id }}</option>
@endforeach
</select>
</div>
</div>
{{-- End form Group --}}
</div>
{{-- End Col md 3 --}}
In my Model for Performance, I have the following relationships :
class Performance extends Model
{
protected $fillable = [
'student_id',
'date',
'last_unit_covered',
'last_page_viewed',
'exam_type',
'exam_score',
'comments'
];
public function student() {
return $this->belongsTo(User::class, 'student_id', 'id');
}
public function level() {
return $this->belongsTo(User::class, 'level_id', 'id');
}
public function company() {
return $this->belongsTo(Company::class);
}
}
Users Example data id 1 name john doe company_id 4 Level_id 5 teacher_id 3 id 3 name jane doe company_id 4 Level_id 25 teacher_id 3
Levels Example data id 1 name Survival id 2 name Elementary id 3 name Pre-intermediate id 4 name Intermediate id 5 name Upper Intermediate id 6 name Advanced
in my performance controller I have:
public function PerformanceAdd() {
$data['students'] = User::where('teacher_id', Auth::user()->id)->get();
$data['levels'] = Level::all();
return view('admin.performance.performance_add', $data);
}
Any Help appreciated. Thanks
@ErikRobles yes with the on change you could always set the .innerhtml or . Value with JS if you already have it . I have this code in my app which swaps depending on the field type you select
<div class="form-group">
<label
for="{{str_replace(' ', '_', strtolower($field->name))}}">{{$field->name}}</label>
@switch($field->type)
@case('Text'):
<input type="text"
class="form-control
<?php if ($errors->has(str_replace(' ', '_', strtolower($field->name)))) {?>border border-danger<?php }?>"
name="{{str_replace(' ', '_', strtolower($field->name))}}"
value="{{ old(str_replace(' ', '_', strtolower($field->name)))}}"
>
@break
@case('Textarea')
<textarea
name="{{str_replace(' ', '_', strtolower($field->name))}}"
cols="30"
rows="10"
class="form-contol
<?php if ($errors->has(str_replace(' ', '_', strtolower($field->name)))) {?>border-danger<?php }?>">{{ old(str_replace(' ', '_', strtolower($field->name)))}}
</textarea>
@break
@case('Select')
<?php $array = explode("\r\n", $field->value);?>
<select
name="{{str_replace(' ', '_', strtolower($field->name))}}"
class="form-control <?php if ($errors->has(str_replace(' ', '_', strtolower($field->name)))) {?>border-danger<?php }?>">
@foreach($array as $id=>$key)
<option
value="{{ $key }}" @if(old(str_replace(' ', '_', strtolower($field->name))) == $key){{ 'selected'}}@endif>{{ $key }}</option>
@endforeach
</select>
@break
@case('Checkbox')
<?php $array = explode("\r\n", $field->value);?>
<?php $values = explode(",", old(str_replace(' ', '_', strtolower($field->name))));?>
@foreach($array as $id=>$key)
<br><input type="checkbox"
name="{{str_replace(' ', '_', strtolower($field->name))}}[]"
value="{{ $key }}"
@if(in_array($key, $values)){{ 'checked'}}@endif>
<label> {{ $key }}</label>
@endforeach
@break
@default
<input type="text"
class="form-control <?php if ($errors->has(str_replace(' ', '_', strtolower($field->name)))) {?>border-danger<?php }?>"
name="{{str_replace(' ', '_', strtolower($field->name))}}"
placeholder="{{ $field->name }}"
value="{{ old(str_replace(' ', '_', strtolower($field->name)))}}">
@endswitch
</div>
Please or to participate in this conversation.