Error in Sending form data to Django views through AJAX - json

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.

Related

Button type submit Vs Input type submit | laravel 8

I have a form to register a student.
The form submits when i use the following code
<div class="text-xs-right">
<i class="ti-save-alt"></i> <input type="submit" class="btn btn-info btn-rounded mb-5" value="Register">
</div>
But it just refreshes if i use the Button type Submit (below code)
<button type="submit" value="Submit" class="btn btn-rounded btn-primary btn-outline">
<i class="ti-save-alt"></i> Register
</button>
Can someone please help me understand what am i doing wrong here?
I tried to check if there is any jquery/Js Conflicts by commenting out the scripts. Nothing works.
I need to have the button working, as i am using fa icons to style them.
Thank in advance.
My full form code
<form class="form" method="post" action="{{ route('learner.store') }}">
#csrf
<div class="box-body">
<h4 class="box-title text-info"><i class="ti-user mr-15"></i> Personal Info</h4>
<hr class="my-15">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>CPR Number</label>
<input type="text" name="cprnumber" class="form-control" placeholder="CPR Number" required data-validation-required-message="This field is required">
</div>
#error('cprnumber')
<span class="text-info">{{ $message }}</span>
#enderror
</div>
<div class="col-md-4">
<div class="form-group">
<label>First Name</label>
<input type="text" name="firstname" class="form-control" placeholder="First Name">
</div>
#error('firstname')
<span class="text-info">{{ $message }}</span>
#enderror
</div>
<div class="col-md-4">
<div class="form-group">
<label>Middle Name</label>
<input type="text" name="middlename" class="form-control" placeholder="Middle Name">
</div>
#error('middlename')
<span class="text-info">{{ $message }}</span>
#enderror
</div>
<div class="col-md-4">
<div class="form-group">
<label>Last Name</label>
<input type="text" name="lastname" class="form-control" placeholder="Last Name">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>Gender</label>
<select name="learnergender" id="select" required class="form-control">
<option value="">Select</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Date of Birth</label>
<input type="date" name="learnerdob" class="form-control" required data-validation-required-message="This field is required">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Nationality</label>
<select name="learnernationality" id="select" required class="form-control">
<option value="">Select</option>
<option value="Bahraini">Bahraini</option>
<option value="Non-Bahraini">Non Bahraini</option>
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Marital Status</label>
<select name="learnermaritalstatus" id="select" required class="form-control">
<option value="">Select</option>
<option value="Single">Single</option>
<option value="Married">Married</option>
<option value="Divorced">Divorced</option>
<option value="Widowed">Widowed</option>
</select>
</div>
</div>
</div>
<h4 class="box-title text-info"><i class="ti-save mr-15"></i> Contact Details</h4>
<hr class="my-15">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>E-mail</label>
<input type="email" name="learneremail" class="form-control" placeholder="E-mail">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Mobile Number</label>
<input type="text" name="learnermobile" class="form-control" placeholder="Phone">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>Building Number</label>
<input type="text" name="buildingnumber" class="form-control" placeholder="Building Number">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Road Number</label>
<input type="text" name="roadnumber" class="form-control" placeholder="Road Number">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Block Number</label>
<input type="text" name="blocknumber" class="form-control" placeholder="Block Number">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Area</label>
<input type="text" name="area" class="form-control" placeholder="Area">
</div>
</div>
</div>
<h4 class="box-title text-info"><i class="ti-save mr-15"></i> Education Details</h4>
<hr class="my-15">
<div class="row">
<div class="col-md-2">
<div class="form-group">
<label>Education Level</label>
<select name="educationlevel" id="select" required class="form-control">
<option value="">Select</option>
<option value="Primary">Primary</option>
<option value="Preparatory">Preparatory</option>
<option value="Secondary">Secondary</option>
<option value="Diploma">Diploma</option>
<option value="Bachelor">Bachelor</option>
<option value="Masters/PHD">Masters/PHD</option>
</select>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Grade</label>
<input type="text" name="learnergrade" class="form-control" placeholder="Grade">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Graduation Date</label>
<input type="date" name="graduationdate" class="form-control" required data-validation-required-message="This field is required">
</div>
</div>
</div>
<h4 class="box-title text-info"><i class="ti-save mr-15"></i> Organisation Details</h4>
<hr class="my-15">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Organisation</label>
<input type="text" name="organisation" class="form-control" placeholder="Organisation">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Office E-mail</label>
<input type="email" name="officeemail" class="form-control" placeholder="Office E-mail">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Office Number</label>
<input type="text" name="officenumber" class="form-control" placeholder="Office Phone">
</div>
</div>
</div>
<div class="row">
<div class="col-md-2">
<div class="form-group">
<label>Position</label>
<input type="text" name="position" class="form-control" placeholder="Position">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Department</label>
<input type="text" name="department" class="form-control" placeholder="Department">
</div>
</div>
</div>
<div class="form-group">
<label>Description</label>
<textarea rows="5" name="description" class="form-control" placeholder="Description/Remarks/Notes"></textarea>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<!-- <button type="button" class="btn btn-rounded btn-warning btn-outline mr-1">
<i class="ti-trash"></i> Cancel
</button>
<button type="submit" value="Submit" class="btn btn-rounded btn-primary btn-outline">
<i class="ti-save-alt"></i> Register
</button> -->
<div class="text-xs-right">
<input type="submit" class="btn btn-info btn-rounded mb-5" value="Register">
</div>
</div>
</form>
Store method
public function LearnerManagementStore(Request $request){
$validatedData =$request->validate([
'cprnumber' => 'required|unique:learner_management',
'firstname' => 'required',
'lastname' => 'required',
'learnerdob' => 'required',
'learneremail' => 'required',
'learnermobile' => 'required',
]);
$data = new LearnerManagement();
$data->cprnumber = $request->cprnumber;
$data->firstname = $request->firstname;
$data->middlename = $request->middlename;
$data->lastname = $request->lastname;
$data->learnergender = $request->learnergender;
$data->learnerdob = $request->learnerdob;
$data->learnernationality = $request->learnernationality;
$data->learnermaritalstatus = $request->learnermaritalstatus;
$data->learneremail = $request->learneremail;
$data->learnermobile = $request->learnermobile;
$data->buildingnumber = $request->buildingnumber;
$data->roadnumber = $request->roadnumber;
$data->blocknumber = $request->blocknumber;
$data->area = $request->area;
$data->educationlevel = $request->educationlevel;
$data->learnergrade = $request->learnergrade;
$data->graduationdate = $request->graduationdate;
$data->organisation = $request->organisation;
$data->officeemail = $request->officeemail;
$data->officenumber = $request->officenumber;
$data->position = $request->position;
$data->department = $request->department;
$data->description = $request->description;
$data->save();
$notification = array (
'message' => 'Learner Added successfully',
'alert-type' => 'success'
);
return redirect()->route('learners.viewall')->with($notification);
}
Route
// Learner Management
Route::prefix('learner')->group(function(){
Route::get('/viewall', [LearnerController::class, 'LearnerManagementViewall'])-> name('learners.viewall');
Route::get('/view/{id}', [LearnerController::class, 'LearnerManagementView'])-> name('learner.view');
Route::get('/add', [LearnerController::class, 'LearnerManagementAdd'])-> name('learner.add');
Route::post('/store', [LearnerController::class, 'LearnerManagementStore'])-> name('learner.store');
Route::get('/edit/{id}', [LearnerController::class, 'LearnerManagementEdit'])-> name('learner.edit');
Route::post('/update/{id}', [LearnerController::class, 'LearnerManagementUpdate'])-> name('learner.update');
Route::get('/delete/{id}', [LearnerController::class, 'LearnerManagementDelete'])-> name('learner.delete');
});

Laravel: How To Print Ticket Using Laravel After Adding Data?

Good day pips. I am struggling on printing after I click create.
the ui for creating and printing
When I click the create button, it will add to the database and print a number or ticket. All I know is dompdf but that one is downloading pdf
<div class="modal fade" id="callModal" tabindex="-1" role="dialog" aria-labelledby="callModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="callModalLabel">New Call</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<!--start of the form-->
<form id="form-data" class="form-horizontal" method="POST" action="{{ route('call.store') }}">
{{ csrf_field() }}
<div class="row form-group">
<div class="col-md-12">
<input type="hidden" class="form-control" id="trans-id" name="trans_id" readonly>
</div>
<div class="col-md-12">
<label>Transaction Type:</label>
<input type="text" class="form-control" id="trans-name" readonly>
</div>
<div class="col-md-12">
<input type="hidden" id="called" name="called" value="NO">
</div>
<div class="col-md-12">
<label>Department:</label>
<select class="form-control" id="dept_id" name="dept_id" required>
<option value="" data-hidden="true"
selected="selected">-- Select Department --
</option>
#foreach($departments as $department)
<option value= "{{ $department->id }}">
{{ $department->dept_name }}
</option>
#endforeach
</select>
</div>
<div class="col-md-12">
<label>RFID</label>
<input type="text" id="rfid" class="form-control" name="rfid" required>
</div>
<div class="col-md-12">
<label>Student First Name</label>
<input type="text" id="firstname" class="form-control" name="firstname" required="">
</div>
<div class="col-md-12">
<label>Student Last Name</label>
<input type="text" id="lastname" class="form-control" name="lastname" required>
</div>
<div class="col-md-12">
<label>Amount</label>
<input type="number" id="amount" class="form-control" name="amount" step=".01" required>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success btn-fill pull-right" id="form-button-add" name="form-button-add" onclick="window.print()">
CREATE <!--CREATE, SAVE AND PRINT THE TICKET-->
</button>
<button data-dismiss="modal" aria-hidden="true" class="btn btn-basic pull-right" style="margin-right: 2%">
CANCEL
</button>
<div class="clearfix"></div>
</form>
<!-- end of the form -->
</div>
</div>
</div>
</div>
I already code the javascript onclick in my create button. My problem is it prints the current page of the ui. I just want to print the details such as C-1, counter 1. Another problem is that is show the preview but it doesn't add on the database.

How to store data to browser cache directory in html?

The Project: https://github.com/marcosnunes/storeData
I need to publish into {div id="result"}{/div} the data from the form and then store to local cached directory. Would anyone help me to write this code? What I am trying to do is make a local site for to be loaded into an Android Hibrid App.
The form:
<body>
<div class="w3-container">
<div class="w3-card-4" style="width:100%">
<header class="w3-container w3-light-grey">
<h2><Div id="nameBox"></Div></h2>
</header>
<div class="w3-container">
<h2><div id="result"></div></h2>
</div>
</div>
</div>
<div class="w3-container">
<div class="row">
<div class="col-md-8">
<h3>Adicionar um Envelope e Saldo</h3>
<form name="contact-form"
method="post" id="contact-form">
<div class="form-group">
<label for="inputName">Envelope</label>
<input type="text"
class="form-control" id="cardName" name="cardName" placeholder="Envelope"
required>
</div>
<div class="form-group">
<label for="inputValue">Valor</label>
<input type="number" class="form-control" id="Value" name="Value"
placeholder="Valor" required>
</div>
<div class="form-group">
<label for="inputDescription">Descrição</label>
<input type="text" class="form-control" id="Description"
name="Description"
placeholder="Descrição" required>
</div>
<button onclick="store()" class="btn btn-primary" name="submit"
value="Submit"
id="submit_form">Adicionar</button>
</form>
</div>
</div>
</div>
</body>
</html>
I need too create new div on the button onclick()
The java code
<script>
function store(){
if (typeof(Storage) !== "undefined") {
var inputName = document.getElementById("cardName");
sessionStorage.setItem("cardName", inputName.value);
var inputValue = document.getElementById("Value");
sessionStorage.setItem("Value", inputValue.value);
var inputDescription = document.getElementById("Description");
sessionStorage.setItem("Description", inputDescription.value);
document.getElementById("result1").innerHTML = sessionStorage.getItem("cardName");
document.getElementById("result2").innerHTML = sessionStorage.getItem("Value");
document.getElementById("result3").innerHTML = sessionStorage.getItem("Description");
} else {
document.getElementById("result").innerHTML = "Ops! Alguma coisa deu errado...";
}
}
I got it !
Just inserted type="button" on button.
<button onclick="store()" **type="button"** class="btn btn-primary"
name="submit" value="Submit"
id="submit_form">Adicionar</button>

Alignment of label and textbox

I am facing this issue again with the alignment which I am unable to rectify. I don't know what I am missing out. Please find the plunkr [https://plnkr.co/edit/UD0YZQhgOjIoJ1nqg7sb?p=preview] .Please maximise the plunkr output for the actual output I am getting. Any help people?
Html code:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div ng-controller="headerCtrl">
<div class="container" style="padding-top:20px;">
<div ng-app="TimeSheet" data-ng-controller="headerCtrl" class="container" style="margin-top:60px">
<div ng-show="error" class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span>
</button>
<p>{{ error }}</p>
</div>
<!--<p><a data-ng-click="showadd()" href="javascript:;" class="btn btn-primary">Generate Report</a></p>-->
<div class="panel panel-default">
<div class="panel-body">
<section>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="form-horizontal">
<div class="form-group">
<label for="title" class="col-sm-5 control-label">UserID</label>
<div class="col-sm-4">
<select data-ng-model="user.userid" class="form-control" id="title" required>
<option value="" selected="selected">(Select User ID)</option>
<option value="user1">user1</option>
<option value="user2">user2</option>
<option value="user3">user3</option>
<option value="user4">user4</option>
</select>
<!--<input type="text" data-ng-model="user.userid" class="form-control" id="title" placeholder="Enter your User ID" required title="Enter your UserID" />-->
</div>
</div>
<div class="form-group">
<label for="title" class="col-sm-5 control-label">Status</label>
<div class="col-sm-4">
Active
<input name="Status" type="radio" data-ng-model="user.ResultStatus" value="Active" required title="Choose status"> In-Active
<input name="Status" type="radio" data-ng-model="user.ResultStatus" value="InActive" required title="Choose status">
</div>
</div>
<div class="form-group">
<label for="startDate" class="col-sm-5 control-label">Date</label>
<div class="col-sm-4">
<input name="startDate" data-ng-model="user.date" id="startDate" class="date-picker" required />
<!--<input type="text" data-ng-model="user.projectid" class="form-control" id="title" placeholder="Enter your Project ID" required title="Enter your ProjectID" />-->
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-4">
<span data-ng-hide="editMode">
<input type="submit" value="Generate" ng-disabled="adduserform.$invalid" data-ng-click="add()" class="btn btn-primary" />
</span>
<span data-ng-show="editMode">
<input type="submit" value="Update" ng-disabled="adduserform.$invalid" data-ng-click="update()" class="btn btn-primary" />
</span>
<input type="button" value="Cancel" data-ng-click="cancel()" class="btn btn-primary" />
</div>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
you can see the alignment issue here in this image below
To make the date input span the whole width, you need the .form-control class on it.
<input name="startDate" data-ng-model="user.date" id="startDate" class="form-control date-picker" required />
Also note that the <input> tag must have a type attribute with it.
You have a container within a container for your timesheet div. Removing the .container class from the timesheet div fixes the alignment issue.
Also, put the <table> in a row and a column:
<div class="row">
<div class="col-md-12">
<!-- Your table here -->
<table class="table table-hover">
...
</table>
</div>
</div>
See it working here.

ng-click not fire function in controller

The bug is very weird, I have been stucking here for hours just can find why my function in controller is fired.
Here is my HTML code:
<div class="col-md-9 clearfix" id="customer-account" ng-controller='ProfileController'>
<div class="box clearfix">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="password_old">oldpassword</label>
<input type="password" class="form-control" id="password_old" ng-model="passwordold">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="password_1">newpassword</label>
<input type="password" class="form-control" id="password_1" ng-model="passwordnew1">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="password_2">retype</label>
<input type="password" class="form-control" id="password_2"ng-model="passwordnew2">
</div>
</div>
<p>{{passwordnew2}}</p>
</div>
<!-- /.row -->
<div class="text-center">
<button type="submit" class="btn btn-primary">
<i class="fa fa-save" ng-click="changePwd(passwordold,passwordnew1,passwordnew2)"></i> save
</button>
</div>
<div class="text-center" >
<p>{{errorMessageChangepwd}}aaaa</p>
</div>
</div>
</div>
After I click the Button, which ng-click attribute as you can see, nothing happen.
Here is my controller code:
controller('ProfileController', ['$scope','UserFactory','SharedDataFactory',
function($scope,UserFactory,SharedDataFactory){
$scope.user = UserFactory.user;
$scope.passwordold;
$scope.errorMessageChangepwd='error';
$scope.showErrMsgChangepwd = false;
$scope.passwordnew1;
$scope.passwordnew2;
$scope.changePwd = function(passwordold,passwordnew1,passwordnew2){
console.log("aaaaaaaaaa");
if (passwordnew1!==passwordnew2){
$scope.showErrMsgChangepwd= true;
$scope.errorMessageChangepwd = 'error';
}else{
UserFactory.changePwd(passwordnew1)
.catch(function(err){
console.log(err.data);
}).then(function(response){
console.log(response);
});
}
};}]);
I called console.log("aaaaaaaaaa"); in the first line of my function, but after I click the button, nothing is shown on console.
And also
<div class="text-center" >
<p>{{errorMessageChangepwd}}aaaa</p>
</div>`
does not show error aaaa as expected but aaa on the browser.
what could be wrong?
Thanks.
You need to add ng-controller="ProfileController" in the top of your form.
Which looks like missing on your code.