what is that buttonid thing?
Edit using Modal Window
Hey everyone! Following Jeffrey's Laravel From Scratch course I was able to learn a lot of laravel (thanks). However, at this moment, I'm stuck with this problem.
So I have this Employment History on the user profile. I was able to set up the creating part so the users can add employment history. I'm doing this using a modal window. Now, I would like to let users edit (and delete probably) what they entered so I have an edit button which leads to a modal window.
Here's how I display the records from the database.
@foreach ($employment as $empl)
<button data-toggle="modal" data-target="#edit-empl" href="#edit-empl" class="btn btn-default editbtn-modal" value="{{ $empl->id }}" type="button" name="editbtn">Edit</button>
<h3 class="profile-subtitle">{{ $empl->company }}</h3>
<p class="profile-text subtitle-desc">{{ $empl->parseDate($empl->from) }} - {{ $empl->parseDate($empl->to) }}</p>
@endforeach
As you can see, I have this button with a value of $empl->id. I did this to get the employment id from the db. When I click the edit button I open this modal window:
<form class="app-form" action="/profile/employment/edit/{id}" method="POST">
{{ csrf_field() }}
<input class="editID" type="hidden" name="editID" value="">
@foreach ($employment as $empl)
@if ($empl->id == buttonidhere)
<div class="form-group">
<label for="company">Company:</label>
<input type="text" name="company" value="{{ $empl->company }}">
</div>
<div class="form-group">
<label for="month">From:</label>
<input type="date" name="from" value="{{ $empl->from }}">
</div>
<div class="form-group">
<label for="to">To:</label>
<input type="date" name="to" value="{{ $empl->to }}">
</div>
@endif
@endforeach
<div class="row">
<div class="col-sm-6">
<input type="submit" class="btn btn-primary profile-form-btn" value="Save Changes">
</div>
</div>
</form>
As you can see, in this modal I have a foreach to go through the records and I wanted to check if the $empl->id matches the value from the button id I've clicked.. And this is where I'm stuck.. I don't know how to make this verification, how to get that button value so I can perform the if statement and get the record with the corresponding id.
PS: I tried passing the value (using jquery) to this hidden input, which worked but didn't helped me at all because I can't take the input value without submitting the form.. I guess it works for the updating method in the controller but right now I'm focused on displaying the correct record based on id..
Can you guys help? I'm stuck on this for hours haha..
well you have to declare your modal inside of a foreach loop, you see
that way you will have as many modals and forms with the correct id for that object
@foreach($employment as $empl)
<form class="app-form" action="/profile/employment/edit/$empl->id" method="POST">
{{ csrf_field() }}
<input class="editID" type="hidden" name="editID" value="">
<div class="form-group">
<label for="company">Company:</label>
<input type="text" name="company" value="{{ $empl->company }}">
</div>
<div class="form-group">
<label for="month">From:</label>
<input type="date" name="from" value="{{ $empl->from }}">
</div>
<div class="form-group">
<label for="to">To:</label>
<input type="date" name="to" value="{{ $empl->to }}">
</div>
<div class="row">
<div class="col-sm-6">
<input type="submit" class="btn btn-primary profile-form-btn" value="Save Changes">
</div>
</div>
</form>
@endforeach
Please or to participate in this conversation.