save multiple rows into database usin dynamic input fields/larave and jQuery - html

I want to insert multiple rows from dynamic input fields.
So I'm using the following formm and the following script which adds dynamically input fields.
$(document).ready(function() {
$("body").on("click", ".add_new_frm_field_btn", function() {
var random = 1 + Math.floor(Math.random() * 1000); //generate random values..
var index = $(".form_field_outer").find(".form_field_outer_row").length + 1;
//added data-index and outer..class
$(".form_field_outer").append(`<div class="col-12 outer" data-index="${index}_${random}"><div class="card-body form_field_outer_row"> <div class="form-row"><div class="form-group col-md-4"> <label for="inputState">Casting</label><select id="id_casting" class="form-control" name="rows[${index}][id_casting]">
<option selected>Choose...</option><option data-id="{{$casting->id_casting}}" value="{{$casting->id_casting}}">{{$casting->nom.' '.$casting->prenom}}</option> </select></div><div class="form-group col-md-4"><label for="inputState">Type de contrat</label><select id="id_modele_contrat" class="form-control" name="rows[${index}][id_modele_contrat]"> <option selected>Choose...</option><option>...</option> </select></div><div class="card-body "><button type="button" class="btn btn-outline-warning mb-1 remove_node_btn_frm_field">Delete</button></div>
</div></div></div> `);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<div class="row">
<div class="col-12">
<div class="card mb-4">
<div class="card-body">
<h5 class="mb-4">Projet</h5>
<!-- <form id="projetform" method="post" action="ajout_projet" class="myForms" enctype="multipart/form-data"> -->
<form method="POST" id="projetform" class="myForms" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="hidden" id="id_projet" name="id_projet" />
<div class="form-group">
<label for="inputAddress">Numéro de projet</label>
<input type="text" class="form-control" id="numero_projet" name="numero_projet" placeholder="Description">
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12 col-md-12 col-xl-8 col-left">
<div class="card mb-4">
<div class="card-body">
<button type="button" class="btn btn-outline-primary mb-1 add_new_frm_field_btn">Ajouter un nouveau casting</button>
</br>
</br>
<div class="row">
<div class="col-12">
<div class="card mb-4 form_field_outer ">
<div class="card-body form_field_outer_row outer" data-index="1">
<input type="hidden" id="id_projet_casting" name="id_projet_casting" />
<div class="form-row">
<div class="form-group col-md-4">
<label for="inputState">Casting</label>
<select class="form-control" name="rows[1][id_casting]" id="id_casting">
<option selected>Choose...</option>
#foreach($castings as $casting)
<option data-id="{{$casting->id_casting}}" value="{{$casting->id_casting}}">{{$casting->nom.' '.$casting->prenom}}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-4">
<label for="inputState">Type de contrat</label>
<select id="id_modele_contrat" class="form-control" name="rows[1][id_modele_contrat]">
<option selected>Choose...</option>
#foreach($models_contrat as $model_contrat)
<option data-id="{{$model_contrat->id_modele_contrat}}" value="{{$model_contrat->id_modele_contrat}}">{{$model_contrat->modele_contrat}}</option>
#endforeach
</select>
</div>
<div class="card-body ">
<button type="button" class="btn btn-outline-warning mb-1 remove_node_btn_frm_field">Delete</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-12 col-md-12 col-xl-4 col-right">
<div class="card mb-4 casting_details ">
<div class="card-body casting_details2 ">
<div class="d-flex flex-row mb-3 ">
<!-- Pictures ot the selected casting will come here-->
</div>
</div>
</div>
</div>
<button type="submit" name="submit" id="submit" class="btn btn-primary mb-1">Add</button>
</div>
Ajax code :
$('#submit').click(function(){
$.ajax({
url:"{{route('ajout_projet.store')}}",
method:"POST",
data:$('#projetform').serialize(),
type:'json',
success: function(e){
console.log(e)
},
error: function(error){
console.log(error)
}
});
});
I put in my controller :
$data = $request->input('rows');
dd($data);
When I add in my frontEnd for example 4 input fields that is four rows , the dd($data); shows only the first row :
array:1 [
1 => array:2 [
"id_casting" => "19"
"id_modele_contrat" => "1"
]
]
while it should show the 4 additional input fields values like :
array:4 [
1 => array:2 [
"id_casting" => "19"
"id_modele_contrat" => "1"
],
2 => array:2 [
"id_casting" => "19"
"id_modele_contrat" => "1"
],
........
]
I tries many solutions but it stills the same problem n it takes only the first row in consideration and not all the additional input fields.
I am stuck in this point for a week and do not know what to do yet.
what i know is the problem is in my html or jquery code but i don't know exactly what is triggering this problem.
please help
UPDATE
migration files :
public function up()
{
Schema::create('projets', function (Blueprint $table) {
$table->increments('id_projet');
$table->string('numero_projet',10);
}
public function up()
{
Schema::create('projets_castings', function (Blueprint $table) {
$table->increments('id_projet_casting');
$table->integer('id_projet')->nullable()->unsigned();
$table->integer('id_casting')->nullable()->unsigned();
$table->integer('id_contrat')->nullable()->unsigned();
$table->integer('actif')->default('1');
$table->timestamps();
$table->foreign('id_projet')->references('id_projet')->on('projets');
$table->foreign('id_casting')->references('id_casting')->on('castings');
$table->foreign('id_contrat')->references('id_contrat')->on('contrats');
});
}

maybe that can help someone else
I solved my problem by changing my html to
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<form method="POST" id="projetform" class="myForms" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="row">
<div class="col-12">
<div class="card mb-4">
<div class="card-body">
<h5 class="mb-4">Projet</h5>
<!-- <form id="projetform" method="post" action="ajout_projet" class="myForms" enctype="multipart/form-data"> -->
<input type="hidden" id="id_projet" name="id_projet" />
<div class="form-group">
<label for="inputAddress">Numéro de projet</label>
<input type="text" class="form-control" id="numero_projet" name="numero_projet" placeholder="Description">
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12 col-md-12 col-xl-8 col-left">
<div class="card mb-4">
<div class="card-body">
<button type="button" class="btn btn-outline-primary mb-1 add_new_frm_field_btn">Ajouter un nouveau casting</button>
</br>
</br>
<div class="row">
<div class="col-12">
<div class="card mb-4 form_field_outer ">
<div class="card-body form_field_outer_row outer" data-index="1">
<input type="hidden" id="id_projet_casting" name="id_projet_casting" />
<div class="form-row">
<div class="form-group col-md-4">
<label for="inputState">Casting</label>
<select class="form-control" name="rows[1][id_casting]" id="id_casting">
<option selected>Choose...</option>
#foreach($castings as $casting)
<option data-id="{{$casting->id_casting}}" value="{{$casting->id_casting}}">{{$casting->nom.' '.$casting->prenom}}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-4">
<label for="inputState">Type de contrat</label>
<select id="id_modele_contrat" class="form-control" name="rows[1][id_modele_contrat]">
<option selected>Choose...</option>
#foreach($models_contrat as $model_contrat)
<option data-id="{{$model_contrat->id_modele_contrat}}" value="{{$model_contrat->id_modele_contrat}}">{{$model_contrat->modele_contrat}}</option>
#endforeach
</select>
</div>
<div class="card-body ">
<button type="button" class="btn btn-outline-warning mb-1 remove_node_btn_frm_field">Delete</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
<button type="submit" name="submit" id="submit" class="btn btn-primary mb-1">Add</button>
</div>

Related

Angular input form values only update after focus

I have an Angular app with this form named 'form' and this page also has another form in the same html called 'form' as well. The second form loads the row first values coming from the ngFor but the rest only load after I click inside the unloaded input. This might be because of the same name?
<div class="all">
<div class="card card-rounded shadow-sm">
<form [formGroup]="form">
<div class="p-3">
<div class="form-row">
<div class="form-group col-md-12">
<label>Tema</label>
<input [ngClass]="cssValidator(f.theme)" class="form-control" formControlName="theme">
<div *ngIf="f.theme.errors?.required && f.theme.touched" class="invalid-feedback">
Obrigatório
</div>
<div *ngIf="f.theme.errors?.maxlength" class="invalid-feedback">
Máximo 50 letras
</div>
</div>
</div>
</div>
</form>
</div>
<div class="card rounded shadow-sm mt-4" *ngIf="editMode">
<div class="p-3">
<div class="d-flex border-bottom">
</div>
<div class="form-row p-1">
<div [formGroup]="form" class="col">
<div formArrayName="details" *ngFor="let detail of details.controls; let i = index">
<fieldset [formGroupName]="i" class="form-group">
<div class="row">
<div class="form-group col-md-4">
<label>Preço</label>
<input [ngClass]="cssValidator(details.get(i+'.price'))" class="form-control" formControlName="price">
</div>
<div class="form-group col-md-4">
<label>Descrição</label>
<input [ngClass]="cssValidator(details.get(i+'.description'))"
class="form-control" formControlName="description" placeholder="Descrição...">
</div>
</div>
</fieldset>
<hr>
</div>
</div>
</div>
</div>
</div>
<br>
</div>
The ts:
get f(): any {
return this.form.controls;
}
get details(): FormArray {
return (this.form.get('details') as FormArray);
}
loadDetails() {
this.detailService.getDetailsByEventId(this.eventId).subscribe(
(detailsResult: Detail[]) => {
detailsResult.forEach(detail => {
this.details.push(this.createDetails(detail));
});
},
(error: any) => {
this.toastr.error('Erro ao tentar carregar detalhes', 'Erro!');
console.log(error);
}
).add(() => this.spinner.hide());
}

Validation Condition In Angular

Hello I'm trying to submit data as per type. There 2 types 1.Answer 2.Sub Flow. But my save button not enabled when I'm selecting sub flow. Please help me What I'm missing there.
BELOW IS THE HTML FILE CODE
<div class="row">
<div class="box">
<app-question-error *ngIf="isError" [errorData]='errorData'></app-question-error>
<form *ngIf="!isError" [formGroup]="questionForm" (ngSubmit)="SubmitForm()">
<div class="col-12 col-md-8 col-sm-12">
<div>
<div class="form-group row">
<label for="colFormLabel" class="col-sm-3 col-form-label">Type </label>
<div class="col-sm-4 d-flex justify-content-between">
<select formControlName="subflow" class="form-control" (change)="onDropDownChange($event)">
<option>Answer</option>
<option>Subflow</option>
</select>
</div>
</div>
</div>
</div>
<div class="col-12 col-md-12 col-sm-12">
<div class="form-group row">
<div class="col-sm-2">
</div>
<div class="col-sm-10">
<div *ngIf="subflowSelection == 'Subflow'">
<ul class="list-group">
<ng-select [items]="flowNames" bindLabel="flowName" appendTo="body" multiple="true"
formControlName="subflow">
</ng-select>
</ul>
</div>
</div>
</div>
</div>
<ng-container *ngIf="subflowSelection === 'Answer'">
<div class="col-12 col-md-8 col-sm-12" formArrayName="answers">
<div *ngFor="let quantity of getAnswers().controls; let i=index" [formGroupName]="i">
<div class="form-group row">
<label for="colFormLabel" class="col-sm-3 col-form-label">{{mylabels.answer}} </label>
<div class="col-sm-9 d-flex justify-content-between">
<input type="text" class="form-control w-90" formControlName="answerText">
<span class="mr-2"></span>
<button (click)="removeAnswers(i)">
<i class="fa fa-minus" aria-hidden="true"></i>
</button>
</div>
</div>
</div>
<div class="col-sm-9 has-error" *ngIf="f.answers.touched && !f.answers.valid">
{{mylabels.answerRequired}}
</div>
<div>
</div>
</div>
<div class="col-12 col-md-8 col-sm-12">
<div class="form-group row">
<label for="colFormLabel" class="col-sm-3 col-form-label"> </label>
<div class="col-sm-9">
<div class="add-more-answers ">
<button type="button" (click)="addAnswers()"
class="col-12 col-md-5 col-sm-12 btn btn-blue">{{mylabels.add_more_ans}}</button>
</div>
</div>
</div>
</div>
</ng-container>
<div class="container">
<div class="col-md-12">
<div class="row d-flex justify-content-end">
<button [routerLink]="['/question-list']" type="button"
class="btn btn-gray btn-sm mr-4">{{mylabels.cancel}}</button>
<button type="submit" [disabled]="!questionForm.valid"
class="btn btn-red btn-sm ">{{mylabels.save}}</button>
</div>
</div>
</div>
</form>
Below is TS file code Where I'm creating form controls.
this.questionForm = this.fb.group({
questionText: ['', Validators.required],
questionHelp: '',
questionNote: '',
remarks:'',
multiAns: [false],
answers: this.fb.array([]),
subflow:'',
});
}
Above file is for Answer and Sub flow Drop down. Can anyone here who can help me for solving this issue. below is the screenshot for that save button.
1.Diabled Save Button after selecting sub flow option
2.Enabled Save Button after selecting Answer Option

Display Dynamic added input fields and images in the same line using jQuery

I'm trying to add dynamically input fields and when I select an element from the selected box it should show an image in the same line of the additional input fields.
So I'm using the following code.
$(document).ready(function() {
$("body").on("click", ".add_new_frm_field_btn", function() {
var random = 1 + Math.floor(Math.random() * 1000); //generate random values..
var index = $(".form_field_outer").find(".form_field_outer_row").length + 1;
console.log(index);
//added data-index and outer..class
$(".form_field_outer").append(
`<div class="col-12 outer" data-index="${index}_${random}">
<div class="card-body form_field_outer_row">
<div class="form-row"><div class="form-group col-md-2">
<label for="inputState">Casting</label>
<select class="form-control maintCostField" name="rows[${index}][id_casting]" id="id_casting" >
<option selected>Choose...</option>
#foreach($castings as $casting)
<option data-id="{{$casting->id_casting}}" value="{{$casting->id_casting}}">{{$casting->nom.' '.$casting->prenom}}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-2"><label for="inputState">Type de contrat</label><select id="id_modele_contrat" class="form-control" name="rows[${index}][id_modele_contrat]"> <option selected>Choose...</option><option>...</option>
#foreach($models_contrat as $model_contrat)
<option data-id="{{$model_contrat->id_modele_contrat}}" value="{{$model_contrat->id_modele_contrat}}">{{$model_contrat->modele_contrat}}</option>
#endforeach</select></div>
<div class="form-group col-md-2">
<label for="date_de_fin_du_contrat">Date de fin du contrat</label>
<div class="input-group date">
<input type="text" class="form-control" id="date_premiere_diffusion" name="date_premiere_diffusion">
<span class="input-group-text input-group-append input-group-addon" id="spanEstPaymentDate">
<i class="simple-icon-calendar"></i>
</span>
</div>
</div><div class="card-body "><button type="button" class="btn btn-outline-warning mb-1 remove_node_btn_frm_field">Delete</button></div>
</div></div></div> `);
$(".form_field_outer").find(".remove_node_btn_frm_field:not(:first)").prop("disabled", false);
$(".form_field_outer").find(".remove_node_btn_frm_field").first().prop("disabled", true);
});
$("body").on("click", ".remove_node_btn_frm_field", function() {
var index = $(this).closest(".outer").data('index')
//get index
$(".casting_details [data-index= " + index + "]").remove() //remove genrated casting_details2 as well
$(this).closest(".outer").remove()
})
$("body").on("change", ".maintCostField", function() {
var index = $(this).closest(".outer").data('index') //get outer div index..
console.log(index);
//check if the data-id not there
if ($(".casting_details [data-index= " + index + "]").length == 0) {
//append new...
$(".casting_details").append(`<div data-index= "${index}" class="casting_details2"> <div class="d-flex flex-row mb-6 casting_details"> <a class="d-block position-relative" href="#"><img src="img/products/marble-cake-thumb.jpg" alt="Marble Cake" class="list-thumbnail border-0" /> <span class="badge badge-pill badge-theme-2 position-absolute badge-top-right">NEW</span></a></div></div> `);
}
/*let id_casting = $(this).find("option:selected").data("id");*/
var id_casting = $(this).find("option:selected").data("id");
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
$.ajax({
url: "getCasting/" + id_casting,
type: "GET",
success: function(html) {
console.log(html);
$(".casting_details [data-index= " + index + "]").html(`<div class='d-flex flex-row mb-6'><a class='d-block position-relative' href='#'><img src="{{ URL::to('/') }}/castingimages/${html.castings[0].photo} " alt='' class='list-thumbnail border-0' /> <div
class="card-body align-self-center d-flex flex-column justify-content-between min-width-zero align-items-lg-center">
<a href="Apps.MediaLibrary.ViewImage.html" class="w-100">
<p class="list-item-heading mb-1 truncate">${html.castings[0].nom} ${html.castings[0].prenom}</p>
</a>
<p class="mb-1 text-muted text-small w-100 truncate">${html.castings[0].date_naissance}</p>
</div></a></div>`);
}
});
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<div class="row">
<div class="col-12">
<div class="card mb-4">
<div class="card-body">
<button type="button" class="btn btn-outline-primary mb-1 add_new_frm_field_btn">Ajouter un nouveau casting</button>
</br>
</br>
<div class="row">
<div class="col-12">
<div class="card mb-4 form_field_outer ">
<div class="card-body form_field_outer_row outer" data-index="1">
<input type="hidden" id="id_projet_casting" name="id_projet_casting" />
<div class="form-row">
<div class="form-group col-md-2">
<label for="inputState">Casting</label>
<select class="form-control maintCostField select2" name="rows[1][id_casting]" id="id_casting">
<option selected>Choose...</option>
#foreach($castings as $casting)
<option data-id="{{$casting->id_casting}}" value="{{$casting->id_casting}}">{{$casting->nom.' '.$casting->prenom}}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-2">
<label for="inputState">Type de contrat</label>
<select id="id_modele_contrat" class="form-control" name="rows[1][id_modele_contrat]">
<option selected>Choose...</option>
#foreach($models_contrat as $model_contrat)
<option data-id="{{$model_contrat->id_modele_contrat}}" value="{{$model_contrat->id_modele_contrat}}">{{$model_contrat->modele_contrat}}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-2">
<label for="date_de_fin_du_contrat">Date de fin du contrat</label>
<div class="input-group date">
<input type="text" class="form-control" id="date_premiere_diffusion" name="date_premiere_diffusion">
<span class="input-group-text input-group-append input-group-addon" id="spanEstPaymentDate">
<i class="simple-icon-calendar"></i>
</span>
</div>
</div>
<div class="card-body ">
<button type="button" class="btn btn-outline-warning mb-1 remove_node_btn_frm_field">Delete</button>
</div>
<div class="casting_details">
<div class="casting_details2">
<div class="d-flex flex-row mb-6">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
The problem is the image is not displayed in the same line as the dynamically added input fields it is displayed shifted from the added line.
I get a result like that :
The second image is shifted from the line added with the input fields.
The problem is in my Html code, if you have any idea please help !

Collapse Cards based on the radio button selection

I have this .cshtml page where I am just adding in a functionality. Based on a radio button selection the cards should collapse. So basically if Existing user selected then collapseone opens, if New user selected collapsetwo opens. I even want to see how can i align both the cards horizontally. Let me add snippets & images.
<div class="login-box align-content-center">
<div class="login-logo">
<b>Registration</b>
</div>
<div class="card" style="width: 600px">
<div class="card-body login-card-body">
<p class="login-box-msg"></p>
<div class="card-header">
<h3 class="card-title">Select User Type</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-sm-6">
<div class="form-group clearfix">
<div class="icheck-primary d-inline">
<input type="radio" id="radioPrimary1" name="r1" checked>
<label for="radioPrimary1">
Existing User
</label>
</div>
<div class="icheck-primary d-inline">
<input type="radio" id="radioPrimary2" name="r1">
<label for="radioPrimary2">
New User
</label>
</div>
</div>
</div>
</div>
</div>
<form action="~/Home/Index" method="post">
<section class="content">
<div class="row">
<div class="col-sm-6">
<div class="card card-primary" id="collapsetwo">
<div class="card-header">
<h3 class="card-title">Existing Company</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
<i class="fas fa-minus"></i>
</button>
</div>
</div>
<div class="card-body">
<div class="form-group">
<label for="inputCompanyList">Company List</label>
<select id="inputCompanyList" class="form-control custom-select">
<option selected disabled>Select one</option>
<option>Company1</option>
<option>Company2</option>
<option>Company3</option>
</select>
</div>
<button type="submit" class="btn btn-primary btn-block">Submit</button>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
<div class="card card-secondary" id="collapsetwo">
<div class="card-header">
<h3 class="card-title">Register</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
<i class="fas fa-minus"></i>
</button>
</div>
</div>
<div class="card-body">
<div class="form-group">
<label for="inputName">Company Name</label>
<input type="text" id="inputName" class="form-control">
</div>
<div class="form-group">
<label for="inputDescription">Zip</label>
<input type="text" id="inputZip" class="form-control">
</div>
<div class="form-group">
<label for="inputCountry">Country</label>
<select id="inputCountry" class="form-control custom-select">
<option selected disabled>Select one</option>
<option>India</option>
<option>Malaysia</option>
<option>Germany</option>
</select>
</div>
<div class="form-group">
<label for="inputCity">City</label>
<select id="inputCity" class="form-control custom-select">
<option selected disabled>Select one</option>
<option>Bangalore</option>
<option>Delhi</option>
<option>Mumbai</option>
</select>
</div>
<button type="submit" class="btn btn-primary btn-block">Search</button>
</div>
<!-- /.card-body -->
</div>
</div>
</div>
</section>
</form>
</div>
</div>
<!--Register Card-->
</div>
Some snaps:
current output:
Expected output:
Collapsing based on the radio button selection. Thanks for your help !!
You can try to add <div class="col-sm-6"> separately outside card1 and card2 so that you can align both the cards horizontally.And you can use js to open collapses.Here is a demo:
html:
<div class="login-box align-content-center">
<div class="login-logo">
<b>Registration</b>
</div>
<div class="card" style="width: 600px">
<div class="card-body login-card-body">
<p class="login-box-msg"></p>
<div class="card-header">
<h3 class="card-title">Select User Type</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-sm-6">
<div class="form-group clearfix">
<div class="icheck-primary d-inline">
<input type="radio" id="radioPrimary1" name="r1" checked onchange="show(this)">
<label for="radioPrimary1">
Existing User
</label>
</div>
<div class="icheck-primary d-inline">
<input type="radio" id="radioPrimary2" name="r1" onchange="show(this)">
<label for="radioPrimary2">
New User
</label>
</div>
</div>
</div>
</div>
</div>
<form action="~/Home/Index" method="post">
<section class="content">
<div class="row">
<div class="col-sm-6">
<div class="card card-primary" id="collapsetwo">
<div class="card-header">
<h3 class="card-title">Existing Company</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
<i class="fas fa-minus"></i>
</button>
</div>
</div>
<div class="card-body collapse show" id="collapse1">
<div class="form-group">
<label for="inputCompanyList">Company List</label>
<select id="inputCompanyList" class="form-control custom-select">
<option selected disabled>Select one</option>
<option>Company1</option>
<option>Company2</option>
<option>Company3</option>
</select>
</div>
<button type="submit" class="btn btn-primary btn-block">Submit</button>
</div>
<!-- /.card-body -->
</div>
</div>
<div class="col-sm-6">
<!-- /.card -->
<div class="card card-secondary" id="collapsetwo">
<div class="card-header">
<h3 class="card-title">Register</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
<i class="fas fa-minus"></i>
</button>
</div>
</div>
<div class="card-body collapse" id="collapse2">
<div class="form-group">
<label for="inputName">Company Name</label>
<input type="text" id="inputName" class="form-control">
</div>
<div class="form-group">
<label for="inputDescription">Zip</label>
<input type="text" id="inputZip" class="form-control">
</div>
<div class="form-group">
<label for="inputCountry">Country</label>
<select id="inputCountry" class="form-control custom-select">
<option selected disabled>Select one</option>
<option>India</option>
<option>Malaysia</option>
<option>Germany</option>
</select>
</div>
<div class="form-group">
<label for="inputCity">City</label>
<select id="inputCity" class="form-control custom-select">
<option selected disabled>Select one</option>
<option>Bangalore</option>
<option>Delhi</option>
<option>Mumbai</option>
</select>
</div>
<button type="submit" class="btn btn-primary btn-block">Search</button>
</div>
<!-- /.card-body -->
</div>
</div>
</div>
</section>
</form>
</div>
</div>
<!--Register Card-->
</div>
js:
<script>
function show(radio) {
if ($(radio).is(':checked')) {
if ($(radio).attr("id") == "radioPrimary1") {
$("#collapse1").addClass("show");
$("#collapse2").removeClass("show");
} else {
$("#collapse1").removeClass("show");
$("#collapse2").addClass("show");
}
}
}
</script>
result:

Error in Sending form data to Django views through AJAX

I have a HTML form, which takes in input from the user. On clicking the submit button, form data needs to be sent to the views.py in a JSON format, through AJAX. I tried out something, but I am getting an error the JSON object must be str, not 'NoneType' . The JSON data is being loaded in the views.py through:
form = json.loads(request.POST.get('form'))
The data is returned in form of a dictionary called 'searchdata'.
The form:
<form method="POST" id="inputForm">
<div class="container" style="margin-bottom: 50px;width:100%">
<div class="container-fluid mt-3">
<div class="card" id="statform">
<div class="card-header">
<div class="card-title">
<div style="font-size: 25px">Filter</div>
</div>
</div>
<br>
<div class="card-body">
<div class="row">
<div class="col-xs-6">
<div class="col-xs-6">
<label name="start_date" class="control-label" style="width:35%">Start date</label>
<input type="date" id="start_date" style="color:black;width:100px" ></input>
</div>
</div>
<div class="col-xs-6">
<label name="end_date" class="control-label" style="width:35%">End Date(Default: Current Date)</label>
<input style="color:black;width:100px" id="end_date" type="date"></input>
</div>
</div>
<br>
<div class="row">
<div class="col-xs-6">
<label name="fruit_name" class="control-label" style="width:35%; padding-left:15px">Select a Fruit</label>
<select class="js-example-placeholder-single1 js-example-basic-multiple form-control" id="fruit_name" placeholder="Select" style="width:210px" multiple="multiple">
</select>
</div>
<div class="col-xs-6">
<label name="vendor_test" class="control-label" style="width:35%">Select Vendor_TEST</label>
<select class="js-example-placeholder-single2 js-example-basic-multiple form-control" style="width:210px" id="vendor_test" multiple="multiple">
</select>
</div>
</div>
<div class="row">
<div class="col-xs-6">
<label name="release_version" class="control-label" style="width:35%; padding-left:15px">Select Release version</label>
<select class="js-example-placeholder-single3 js-example-basic-multiple form-control" style="width:210px" id="release_version" multiple="multiple">
</select>
</div>
<div class="col-xs-6">
<label class="control-label" style="width:35%">Error Message</label>
<input type="text" placeholder="Type message here" style="width:210px">
</div>
</div>
<div class="row">
<div class="col-xs-6">
<label class="control-label" style="width:35%; padding-left:15px">Error Message List</label>
<select class="js-example-placeholder-single4 js-example-basic-multiple form-control" style="width:210px" id = "error_message" multiple="multiple">
</select>
</div>
</div>
<div class="text-center">
<button class="btn btn-md btn-default" type="submit" name="search" value="search" onclick="loadResults()">
<i class="fa fa-arrow-circle-right"></i> Submit
</button>
<button class="btn btn-md btn-default" type="reset" name="searchreset" value="reset">
<i class="fa fa-arrow-circle-right"></i> Reset
</button>
</div>
</div>
</div> <!--form-->
</div> <!-- row -->
</div> <!--container main-->
</form>
The AJAX call I have written:
$.ajax({
url : window.location.href,
type : 'POST',
cache : false,
data:{form:JSON.stringify(formdata)},
success:function(res){
if(typeof res.searchdata != 'undefined'){
self.$dispatch('searchdataevent',res.searchdata);
}
if(typeof res.message != 'undefined'){
self.message = res.message;
}
self.loading = false;
},
error : function(err){
self.message = 'Error communicating with server';
self.loading = false;
}
});
Where am I going wrong? Any help is appreciated.