this is my ajax method that create a form data from an html table
$('#updateStudentInfoBtn').click(function(e){
// var delcare
let studentUUID = $('[name=studentUUID]').val();
let apiUrl = "/api/students/studentUUID";
var formObj = {
firstname : $('[name="firstname"]').val(),
middlename : $('[name="middlename"]').val(),
lastname : $('[name="lastname"]').val(),
gender : $('[name="gender"] option:selected').val(),
dob : $('[name="dob"]').val(),
admission_date : $('[name="admission_date"]').val(),
admission_stage : $('[name="admission_stage"]').val(),
current_stage : $('[name="current_stage"]').val(),
current_section : $('[name="current_section"]').val(),
name_of_guardian : $('[name="name_of_guardian"]').val(),
relationship_to_student : $('[name="relationship_to_student"]').val(),
occupation : $('[name="occupation"]').val(),
phone_no : $('[name="phone_no"]').val(),
};
var form_data = new FormData();
for ( var key in formObj ) {
form_data.append(key, formObj[key]);
}
$.ajax({
method: "PUT",
url: apiUrl,
data: form_data,
processData : false,
contentType : false,
cache: false,
success: function(res){
response = JSON.stringify(res);
if(response.status == 201) {
// show success alert
}
//location.href="/admin/allstudents";
},
error: function(err){
console.log(err);
}
});
});
and this is my php script to retrieve the data sent over put method; have omitted the class part.
public function update($studentUuid = null)
{
$studentModel = new StudentModel();
$studentData = $studentModel->where('uuid', $studentUuid)->first();
$input = $this->request->getRawInput();
return print_r($input);
}
the result of print_r give me this text i dont know how to process the data ::
Array ( [------WebKitFormBoundaryQFcTXWiBZMLhD6ea Content-Disposition:_form-data;_name] => "firstname" Phinehas ------WebKitFormBoundaryQFcTXWiBZMLhD6ea Content-Disposition: form-data; name="middlename" Toast ------WebKitFormBoundaryQFcTXWiBZMLhD6ea Content-Disposition: form-data; name="lastname" Mord ------WebKitFormBoundaryQFcTXWiBZMLhD6ea Content-Disposition: form-data; name="gender" Female ------WebKitFormBoundaryQFcTXWiBZMLhD6ea Content-Disposition: form-data; name="dob" Saturday November 11, 1989 ------WebKitFormBoundaryQFcTXWiBZMLhD6ea Content-Disposition: form-data; name="admission_date" Thursday May 10, 2001 ------WebKitFormBoundaryQFcTXWiBZMLhD6ea Content-Disposition: form-data; name="admission_stage" class 6 ------WebKitFormBoundaryQFcTXWiBZMLhD6ea Content-Disposition: form-data; name="current_stage" form 3 ------WebKitFormBoundaryQFcTXWiBZMLhD6ea Content-Disposition: form-data; name="current_section" yellow ------WebKitFormBoundaryQFcTXWiBZMLhD6ea Content-Disposition: form-data; name="name_of_guardian" DR S.K Sapaa ------WebKitFormBoundaryQFcTXWiBZMLhD6ea Content-Disposition: form-data; name="relationship_to_student" Father ------WebKitFormBoundaryQFcTXWiBZMLhD6ea Content-Disposition: form-data; name="occupation" Manager ------WebKitFormBoundaryQFcTXWiBZMLhD6ea Content-Disposition: form-data; name="phone_no" xxx-xxx-xxxx ------WebKitFormBoundaryQFcTXWiBZMLhD6ea-- )
CodePudding user response:
to get formDAta() use
$this->request->getPost('item');
that works for json or formData request body
$raw=(array)$this->request->getVar()
CodePudding user response:
for testing use postman so donwload it
second if json request set request postman to put
but if formDAta reqeust set request post then add this filed to form data value
var form_data = new FormData();
//this works for you
form_data.append('_method', 'PUT');
https://codeigniter.com/user_guide/incoming/methodspoofing.html
for ( var key in formObj ) {
form_data.append(key, formObj[key]);
}
form_data.append('_method', 'PUT');
$.ajax({
method: "POST",
url: apiUrl,
data: form_data,
processData : false,
contentType : false,
cache: false,
success: function(res){
response = JSON.stringify(res);
if(response.status == 201) {
// show success alert
}
//location.href="/admin/allstudents";
},
error: function(err){
console.log(err);
}
});