I have the code in update() to create a new certificate or update an existign one, and its working.
But then I want that when the user select a specific registration type (radio button), how to show in the textarea the content of the certificate associated with that selected registration type (radiobutton).
But its not workign the "alert(registrationTypeId);" shows the registration type id but this "$('#certificate_content').val( certificate[registrationTypeId] );" dont works.
CertificateController update():
public function update(Request $request){
$registrationType = RegistrationType::where('id', $request->registrationType);
$certificate = $registrationType->certificate;
// if no certificate exists for this type, create it
if(!$certificate ) {
$certificate = new Certificate();
}
$certificate->content = $request->certificate_content;
$certificate->save();
$registrationType->certificate_id = $certificate->id;
$registrationType->save();
Session::flash('success','Certificate configured with success.');
return redirect()->back();
}
Form in the view:
<form method="post" class="clearfix" action="{{route('certificates.update', ['conference_id' => $conference->id])}}" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-row">
<div class="form-group col">
<label>Certificate</label>
@foreach($conference->registrationTypes as $registrationType)
<div class="form-check">
<input
{{ (old('radiobutton') && old('radiobutton') == $rtype->id) ? 'checked' : '' }}
class="form-check-input radio" type="radio" name="registrationType"
value="{{ $registrationType->id }}" id="{{$registrationType->id}}">
<label class="form-check-label" for="exampleRadios1">
Certificate for the registration type "{{$registrationType->name}}"
</label>
</div>
@endforeach
</div>
</div>
<div class="form-group">
<label>Configure the certificate for the selected registration type</label>
<textarea class="form-control" name="certificate_content" id="certificate_content" rows="3">{{ $certificate->content }} {{ old('certificate_content') }} </textarea>
</div>
<div>
<input type="submit" class="btn btn-primary btn" value="Save"/>
</div>
</form>
jQuery:
<script src="https://cloud.tinymce.com/stable/tinymce.min.js"></script>
<script type="text/javascript">
var certificate = {};
@foreach($onference->registrationTypes as $registrationType)
@if(!$registrationType->certificate)
certificate[{{ $registrationType->id }}] = '';
@else
certificate[{{ $registrationType->id }}] = '{{ $registrationType->certificate->content }}';
@endif
@endforeach
$(function() {
$('.radio').change(function() {
var registrationTypeId = $('input[name=registrationType]:checked').val();
alert(registrationTypeId); // shows the registration type id
$('#certificate_content').val( certificate[registrationTypeId] );
});
});
</script>
@stop