When the checkbox "Fill with auth user info" is clicked I want to populate the fields with the auth user info (name and surname) but it's not working. When the checkboxes are clicked is not working the fields are not populated with the auth user info.
What I have for now is in, https://jsfiddle.net/LahL166o/2/ the auth user info appears in this span
<span id="userData" data-name="John"
data-surname="Keane"></span>
The issue should be how to get the ticket type name in the jquery with laravel because as it is ther is no id with "name" or "surname" the result ids are: "namegeneral_1" and "surnamegeneral_1".
laravel form:
<form method="post" id="step1" action="">
{{csrf_field()}}
#if (!empty($allParticipants))
#if($allParticipants == 1)
<p>Fill all fields.</p>
#foreach($selectedRtypes as $k => $selectedRtype)
#foreach(range(1,$selectedRtype['quantity']) as $val)
<h6 class="text-heading-blue mb-3 pb-2 font-weight-bold">Participant - {{$val}} - {{$k}} </h6>
#if ($loop->first)
<div class="form-check">
<input class="form-check-input" id="fill_auth_info" type="checkbox">
<label class="form-check-label d-flex align-items-center" for="exampleRadios1">
<span class="mr-auto">Fill with auth user info.</span>
</label>
</div>
#endif
<span id="userData" data-name="{{ auth()->user()->name }}"
data-surname="{{ auth()->user()->surname }}"></span>
<div class="form-group font-size-sm">
<label for="participant_name" class="text-gray">Name</label>
<input type="text" id="name" name="participant_name[]" required class="form-control" value="">
</div>
<div class="form-group font-size-sm">
<label for="participant_surname" class="text-gray">Surname</label>
<input type="text" id="surname" required class="form-control" name="participant_surname[]" value="">
</div>
#endforeach
#endforeach
#endif
#else
<p>Is not necessary additional info.</p>
#endif
<input type="submit" href="#step2free"
id="goToStep2Free" class="btn btn-primary btn float-right next-step" value="Go to step 2"/>
</form>
Html result:
<form method="post" id="step1form" action="">
<input type="hidden" name="_token" value="">
<h6 class="text-heading-blue mb-3 pb-2 font-weight-bold">Participant - 1 - general </h6>
<div class="form-check">
<input class="form-check-input" name="fill_with_auth_info" id="fill_auth_infogeneral_1"
data-id="general_1" type="checkbox">
<label class="form-check-label d-flex align-items-center"
for="fill_auth_info1">
<span class="mr-auto">Fill with auth user info.</span>
</label>
</div>
<span id="userData" data-name="John"
data-surname="Keane"></span>
<div class="form-group font-size-sm">
<label for="namegeral_1" class="text-gray">Name</label>
<input type="text" id="namegeral_1" name="participant_name[]" required class="form-control" value="">
</div>
<div class="form-group font-size-sm">
<label for="surnamegeneral_1" class="text-gray">Surname</label>
<input type="text" id="surnamegeneral_1" required class="form-control" name="participant_surname[]" value="">
</div>
<input type="hidden" name="all_participants" value="1" />
<input type="hidden" name="rtypes[]" value="1"/>
<h6 class="text-heading-blue mb-3 pb-2 font-weight-bold">Participant - 1 - re </h6>
<div class="form-check">
<input class="form-check-input" name="fill_with_auth_info" id="fill_auth_infore_1"
data-id="re_1" type="checkbox">
<label class="form-check-label d-flex align-items-center"
for="fill_auth_info1">
<span class="mr-auto">Fill with auth user info.</span>
</label>
</div>
<span id="userData" data-name="John"
data-surname="Keane"></span>
<div class="form-group font-size-sm">
<label for="namere_1" class="text-gray">Name</label>
<input type="text" id="namere_1" name="participant_name[]" required class="form-control" value="">
</div>
<div class="form-group font-size-sm">
<label for="surnamere_1" class="text-gray">Surname</label>
<input type="text" id="surnamere_1" required class="form-control" name="participant_surname[]" value="">
</div>
<input type="hidden" name="all_participants" value="1" />
<input type="hidden" name="rtypes[]" value="2"/>
<input type="submit" href="#step2free"
id="goToStep2Free" class="btn btn-primary btn float-right next-step" value="Go to step 2"/>
</form>
jQuery:
let userData = $('#userData');
$("input[name = 'fill_with_auth_info']").on('click', function(e) {
let checked = $(this).is(':checked');
if (checked) {
$('#name').val(userData.data('name'));
$('#surname').val(userData.data('surname'));
} else {
$('#name').val(userData.data(''));
$('#surname').val(userData.data(''));
}
});
I think you may have a number of issues that are preventing this from working. I think most are stemming from your looping and accidentally assigning the same id to many items.
However, the code displayed above is a bit confusing as well - the blade file should produce something different from the HTML you show. I assume this was a later iteration? I will speak to the blade file, as it is easier to see, since its smaller.
To help get this working, I suggest the following: First, you have only one user, so move this:
<span id="userData" data-name="{{ auth()->user()->name }}"
data-surname="{{ auth()->user()->surname }}"></span>
outside of the loop (or put it inside the $loop->first block), so that the #userData id is only assigned once in the DOM.
Next remove the singular id #name from the input. Same for the #surname. The way you have it, it keeps assigning multiple id fields with the same id. The name="participant_name[]" is fine, and will work on POST.
It's not clear if you want the user name & surname to populate ALL the input fields, or just one near the place he checks the box. If you want to populate them all, add a class to the two inputs in the loop like 'firstName' and 'surname'. Then its easy to populate everything with your jquery:
$('.firstName').val(userData.data('name'));
$('.surname').val(userData.data('surname'));
If, however, you wish to populate only the one that is checked, it is a little more complex (but not hard). You will need to have a check box within each loop next to the set of inputs. Easiest way to track the right checkbox against the input fields is to add an iterator into the loop (assign it and then iterate: $x ++) and then use this on the checkbox in a data field (eg data-count={{$x}} ) and now add an id onto the inputs like so:
id = 'name{{$x}}'
id = 'surname{{$x}}'
From here, add to your jquery code to check which box was checked by getting the count from the data-count field on the checkbox. Then, you can identify the right inputs to add the User's name into by id:
var count = $(this).data('count');
$('#name'+count).val(userData.data('name'));
$('#surname'+count).val(userData.data('surname'));
Hope this helps
Related
So i'm trying to save date from this form to my database using laravel but when I test it using print-r
it shows that the form doesn't send anything and I couldn't figure where the problem is ?
View
<form action="{{ route('auctioneer.save') }}" method="POST">
#csrf
<fieldset>
<div class="form-group" style="width: 35%; position: absolute; margin-left: 4%">
<fieldset>
<label class="control-label" for="readOnlyInput">Name</label>
<input class="form-control" id="readOnlyInput" type="text" value="{{ Auth::user()->name }}" readonly="" style="width: 100%">
</fieldset>
</div>
<div class="form-group" style="width: 35%;margin-left: 60%">
<fieldset>
<label class="control-label" for="readOnlyInput">email</label>
<input class="form-control" id="readOnlyInput" type="text" value="{{ Auth::user()->email }}" readonly="" style="width: 100%">
</fieldset>
</div>
<div class="form-group" style="width: 35%; position: absolute; margin-left: 4%">
<label for="entreprisepos">Position dans l'entreprise</label>
<input type="text" class="form-control" required id="entreprisePos" aria-describedby="aucti" placeholder="Commercial ...">
</div>
<div class="form-group" style="width: 35%;margin-left: 60%">
<label for="entrepriseNom">Nom du l'entreprise</label>
<input type="text" class="form-control" required id="entrepriseNom" aria-describedby="aucti" placeholder="Make it Happen ...">
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" id="termsChkbx " type="checkbox" value="" checked="" onchange="isChecked(this, 'sub1')">
I Agree on Terms and Conditions
</label>
</div>
<div class="form-group text-center">
<button type="submit" class="ps-btn" id="sub1" style="width: 20%;" value="Submit">
Submit Form
</button>
</div>
</fieldset>
</form>
Route
Route::post('/submitauct','AuctioneerController#save')->name('auctioneer.save');
Controller
public function save( Request $req){
print_r($req->input());
}
Display
Array ( [_token] => xNY3q1BvlxuT3zylwhtONx0Xv13qVPUhrrga8omo )
you miss the name attribute in tag your data is pass in request but doesnot get any name so you didint see the data
always remamber id is usefull when you work with frontend like js/jquery/css but when you want to work in backend you have to passs name attribute.
next mistake is always use not button when you want to submit form in laravel.
use button when you want to submit form using ajax or axios its good.
hint:in dd() token is print becuase when you use #csrf you get this.
it has by default name property so it shows
pleas if this is helpful vote up..................................................
If you want to send data from HTML form, you need use input attribute name.
<input name="foo" value="bar">
Then you can get the value in Laravel :
request()->input('foo');
I have two tables in my database one practitioner and the second one practitioner_specialty, both of them have fields effective_date and expiry_date. When I use the form to update both if those tables the last effective_date and expiry_date are being saved to both of the tables instead of two separate ones.
Is there a way to block one table from updating so I can update only the second one, or maybe is there a way to save it using different id/name so they will be unique ones for practitioner and practitioner_specialty?
Here are my form and controller used for updating tables.
Controller:
public function updatePractitioner(Request $request, $id)
{
$this->validate($request, [
'effective_date' => 'required',
]
);
$fields = $request->all();
$primary_key = $this->PractitionerRepository->getIdName();
$primary_key_specialty = $this->PractitionerSpecialtyRepository->getIdName();
$practitioner_specialty_id = PractitionerSpecialty::where('practitioner_id', $id)->value('practitioner_specialty_id');
$fields[$primary_key] = $id;
$this->PractitionerRepository->update($fields);
$fields[$primary_key_specialty] = $practitioner_specialty_id;
$this->PractitionerSpecialtyRepository->update($fields);
return back()->with('successUpdate', 'Practitioner has been updated!');
}
update form in blade.php :
<div class="edit-practitioner" style="display: none;">
<form style="box-shadow: none;" action="/practitioner/update/{{$practitioner->practitioner_id}}" method="post"
class="j-pro" id="update-practitioner-form">
{{ csrf_field() }}
<div class="j-content">
<div id="j-row-id" class="j-row">
<div class="row">
<div class="col-sm-12 col-lg-12 col-xl-5">
<div class=" j-unit">
<div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
<span>Practitioner Information</span>
</div>
<label class="j-label">{{trans('personalData.effectiveDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner->effective_date}}"
name="effective_date" id="effective_date">
</div>
<label class="j-label">{{trans('personalData.expiryDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner->expiry_date}}"
name="expiry_date" id="expiry_date">
</div>
<label class="j-label">{{trans('personalData.phoneNumber')}}</label>
<div class="j-input">
</label>
<input type="tel" value="{{$practitioner->phone}}"
name="phone" id="phone">
</div>
<label class="j-label">{{trans('personalData.mobileNumber')}}</label>
<div class="j-input">
<input type="tel" value="{{$practitioner->mobile}}"
name="mobile" id="mobile">
</div>
<label class="j-label">{{trans('personalData.email')}}</label>
<div class="j-input">
<input type="email" value="{{$practitioner->email}}"
name="email" id="email">
</div>
</div>
</div>
<div class="col-xl-1 j-unit"></div>
<div class="col-sm-12 col-lg-12 col-xl-6">
<div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
<span>{{trans('settings.specialty')}}</span>
</div>
<select name="practitioner_specialty_id_update"
id="practitioner_specialty_id_update"
class="form-control-practitioner required">
#foreach($specialties as $specialty)
<option
value="{{$specialty->specialty_id}}">{{$specialty->name}}</option>
#endforeach
</select>
<label class="j-label">{{trans('personalData.effectiveDate')}}</label>
<div class="j-input">
#isset($practitioner_specialty->practitioner_specialty_id)
<input type="date" value="{{$practitioner_specialty->effective_date}}"
name="effective_date" id="effective_date">
#endisset
#empty($practitioner_specialty->practitioner_specialty_id)
<input type="date" name="effective_date" id="effective_date">
#endempty
</div>
<label class="j-label">{{trans('personalData.expiryDate')}}</label>
<div class="j-input">
#isset($practitioner_specialty->practitioner_specialty_id)
<input type="date" value="{{$practitioner_specialty->expiry_date}}"
name="expiry_date" id="expiry_date">
#endisset
#empty($practitioner_specialty->practitioner_specialty_id)
<input type="date" name="expiry_date" id="expiry_date">
#endempty
</div>
</div>
</div>
</div>
<div class="j-divider j-gap-bottom-45 j-gap-top-10"></div>
<button type="submit"
class="btn btn-editpanel btn-success btn-round">Save changes
</button>
<!-- end /.footer -->
<button id="update-cancel-button-practitioner" href="javascript:window.location.href=window.location.href" type="button"
class="btn btn-editpanel btn-danger btn-round">Cancel
</button>
</div>
</form>
</div>
Well you use the same array for both of the tables, hence the reason to update all the fields. You can try to exclude the ones for one form and add them to the other, for example:
$practictionerFields = $request->except('expiry_date', 'effective_date');
$practictionerFields[$primary_key] = $id;
$this->PractitionerRepository->update($practictionerFields);
// and use the other $fields array for the PractitionerSpecialty model.
You can use LOCK query here.
lock is a flag associated with a table. MySQL allows a client session to explicitly acquire a table lock for preventing other sessions from accessing the same table during a specific period. A client session can acquire or release table locks only for itself. It cannot acquire or release table locks for other sessions.
You can read more here: http://mysqltutorial.org/mysql-table-locking
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.
So, I have my SPA at about 98% functional. The app pulls data from a MySQL database and allows the user to edit/delete records. For the page in question, it will edit/delete data of the specified student ID but it will not properly display the data in the text fields.
If you do not input a value it will display the first item in the JSON array but not the one you specify in the search field.
I did not do a very good job at explaining that but here is the HTML page that uses a function to pass data to the controller which then selects the corresponding student by ID and assigns it to the variable $scope.student. I then try to display the student data on the HTML page by using student.first_name (or any property) but it does not work correctly.
<h3>Edit/Delete Student with ID: {{student.student_id}}</h3>
<div class="form-group">
<label for="sid">Student ID:</label>
<input type="text" class="form-control" id="sid" ng-model="sid">
</div>
<p><button type="button" class="btn btn-primary" ng-click="getRecord(sid)">
Get Student Info </button> </p>
<div class='row'>
<div class="col-md-6">
<div class="form-group">
<label for="first_name">First Name:</label>
<input type="text" class="form-control" id="first_name" ng-model="student.first_name">
</div>
<div class="form-group">
<label for="last_name">Last Name:</label>
<input type="text" class="form-control" id="last_name" ng-model="student.last_name">
</div>
<div class="form-group">
<label for="hrs_completed">Hrs Completed:</label>
<input type="text" class="form-control" id="hrs_completed" ng-model= "student.hrs_completed">
</div>
<div class="form-group">
<label for="hrs_attempted">Hrs Attempted:</label>
<input type="text" class="form-control" id="hrs_attempted" ng-model= "student.hrs_attempted">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="gpa_points">GPA Points:</label>
<input type="text" class="form-control" id="gpa_points" ng-model= "student.gpa_points">
</div>
<div class="form-group">
<label for="major">Major:</label>
<input type="text" class="form-control" id="major" ng-model="student.major">
</div>
<div class="form-group">
<label for="advisor_id">Advisor ID:</label>
<input type="text" class="form-control" id="advisor_id" ng-model="student.advisor_id">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" class="form-control" id="email" ng-model="student.email">
</div>
</div>
<button type="button" class="btn btn-primary" ng-click="updateRecord()">Update</button>
<button type="button" class="btn btn-primary" ng-click="deleteRecord()">Delete</button>
And here is my controller on my Javascript page:
app.controller('editCtrl', function($scope, $http) {
$http.get("getStudentData.php")
.then(function (response) {
$scope.students = response.data;
});
$scope.getRecord = function(sid) {
id = sid;
$scope.student = $scope.students.find(s=>s.id == sid);
};
Do I need to make a seperate GET request to the server for this to work properly or do I just need to reference the student object differently?
I think there is mismatch in what you are getting as response.data and what you are trying to .find() and then what you are binding on html.
Check this plunkr.
You are trying to render by searching id property.
$scope.students.find(s=>s.id == sid);
where as you are rendering on UI using student_id property.
{{student.student_id}}
So, surely there is mismatch in what you are getting as response.data and what you are rendering using $scope.student properties. I think my plunkr will show that your function is correct.
In case this answer doesn't work, share your response.data.
I have a page to edit the administrators of a post.
In this page there are some radio buttons. Each radio button corresponds to a administrator of a post. When a radio button is selected the details of that administrator are populated in the form fields.
When the user accesses this edit administrators of a post page no radio button should be checked by default, so I don't have the "checked" property in the radio buttons.
In this page I want to have 2 forms, a form for update and a form to store administrators.
So, for example when the user acesses this page there are for example 3 radio buttons: John, Jake, Create new admin. If the radio button that corresponds to the user John is selected the update form should appear with the form fields populated with the John admin details, the same for the Jake amdin. If the "Create new admin" radio button
is selected it should appear the store form with the form fields reset.
The issue is when the user acesses this edit administrators of a post page no radio button is appearing, so its not possible to select any radio button and so no form appears. Do you know how to organize this two forms properly so that each form appears when the correspondent radio button is selected?
// update form
<form id="update_admins" method="post" class="clearfix"
action="{{route('admins.update', ['post_id' => $post->id])}}" >
{{csrf_field()}}
<div class="form-row">
<div class="form-group col col-lg-6">
<label for="admins">Post admins</label>
#foreach($administrators as $admin)
<div class="form-check">
<input autocomplete="off" class="form-check-input radio" type="radio" name="radiobutton" value="{{ $admin->id }}" id="{{$admin->id}}">
<label class="form-check-label" for="exampleRadios1">
{{$admin->name}}
</label>
</div>
#endforeach
<div class="form-check">
<input autocomplete="off" class="form-check-input" type="radio" name="radiobutton" id="update_admin"
value="update_admin">
<label class="form-check-label" for="exampleRadios2">
Create new admin
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="name">Name</label>
<input autocomplete="off" type="text" required class="form-control" value="{{ $admin->name }}" name="name" id="name">
</div>
<!-- ...the other form fields -->
<input type="submit" class="btn btn-primary btn float-right" value="Update admin"/>
</form>
// store form
<form id="create_admins" method="post" class="clearfix"
action="{{route('admins.store', ['post_id' => $post->id])}}" >
{{csrf_field()}}
<div class="form-row">
<div class="form-group col col-lg-6">
<label for="admins">Post admins</label>
#foreach($administrators as $admin)
<div class="form-check">
<input autocomplete="off" class="form-check-input radio" type="radio" name="radiobutton" value="{{ $admin->id }}" id="{{$admin->id}}">
<label class="form-check-label" for="exampleRadios1">
{{$admin->name}}
</label>
</div>
#endforeach
<div class="form-check">
<input autocomplete="off" class="form-check-input" type="radio" name="radiobutton" id="store_admin"
value="store_admin">
<label class="form-check-label" for="exampleRadios2">
Create new admin
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="name">Name</label>
<input autocomplete="off" type="text" required class="form-control" value="{{ old('name') }}" name="name" id="name">
</div>
<!-- ...the other form fields -->
<input type="submit" class="btn btn-primary btn float-right" value="Store admin"/>
</form>
JS:
$(document).ready(function () {
// receive array with administrators details from the AdministratorController
var admins = {!! $administrators !!}
$("#create_admins").hide();
$("#update_admins").hide();
$("input[name='radiobutton']").click(function() {
let id = $(this).attr("id");
if (id == "store_admin") {
$("input[name='name']").val("");
} else {
let data = admins.find(e => e.id == id) || {
name: "",
...
};
$("input[name='name']").val(data.name);
...
}
});
});
Recognize the forms by controller action
#if(\Route::getCurrentRoute()->getActionMethod() == 'create')
//show create form
#else
//show edit form
#endif