I have a form that allows users to edit the administrators of a post but also create new administrators for the post.
So this form has some radio buttons. Each radio button corresponds to an administrator of a post. When some radio button (administrator) is clicked the details of the administrator are populated in the form fields. When the "Update" button has clicked the details of the administrator are updated.
But then there is a static radio button, "Create new administrator", that allows creating a new administrator. When this radio button is selected the form fields are reset so the user can insert values to create a new admin when it clicks on the "Store" button.
This first part was working fine.
But then I needed to add some jQuery to change the form action based on radio button selection, so its possible to update but also create administrators when the "Create new admin" radio button is selected.
But when adding this code "form.attr('action', '{{route('admins.store', ['post_id' => $post->id])}}');" and "form.attr('action', '{{route('admins.update', ['post_id' => $post->id])}}');" that first part above stops working.
And in the console it appears the error "form is not defined" on this line "form.attr('action', '{{route('admins.store', ['post_id' => $post->id])}}');" .
Do you know why?
jQuery:
- var admins = {!! $administrators !!}
- $("input[name='radiobutton']").click(function() {
- if($(this).attr("id") == "create_administrator"){
- $("#adminUpdateButton").hide();
- $("#adminStoreButton").show();
- form.attr('action', '{{route('admins.store', ['post_id' => $post->id])}}');
- }
- else{
- $("#adminUpdateButton").show();
- $("#adminStoreButton").hide();
- form.attr('action', '{{route('admins.update', ['post_id' => $post->id])}}');
- }
- let id = $(this).attr("id");
- let data = administrators.find(e => e.id == id) || {
- name: "",
- email: "",
- ...: ""
- };
- $("input[name='name']").val(data.name);
- ...
- });
Form:
- <form method="post" class="clearfix" action="{{route('admins.update', ['id' => $post->id])}}" enctype="multipart/form-data">
- {{csrf_field()}}
- @foreach($administrators $admin)
- <div class="form-check">
- <input class="form-check-input" type="radio" name="radiobutton" id="{{$admin->id}}" value="">
- <label class="form-check-label" for="">
- {{$admin->name}}
- </label>
- </div>
- @endforeach
- <div class="form-check">
- <input class="form-check-input" type="radio" name="radiobutton" id="create_administrator"
- value="option2">
- <label class="form-check-label" for="exampleRadios2">
- Create new administrator
- </label>
- </div>
- <div class="form-group">
- <label for="name">Name</label>
- <input type="text" required class="form-control" value="{{ $admin->name }}" name="name">
- </div>
- <!-- below I have more form fields like administrator name, email, etc -->
- <input type="submit" id="adminStoreButton" class="btn btn-primary" value="Create"/>
- <input type="submit" id="adminUpdateButton" class="btn btn-primary" value="Update"/>
- </form>