Osborne Mordreds
Osborne Mordreds

Reputation: 3

How Can I Process Data From "$this->request->getRawInput()" CodeIgniter4

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-- )

Upvotes: 0

Views: 2464

Answers (3)

ClicusBuckis
ClicusBuckis

Reputation: 41

I figured it out, no idea if it is the right solution for every case but it works for me... I created a helper file (to hide the ugliness) which extracts the values into a regular associative array:

put_helper.php:

function getPut($raw_input)
{
    $input_string = $raw_input[array_keys($raw_input)[0]];

    preg_match('#\{(.*?)\}#', $input_string, $match);

    $json_string = '{' . $match[1] . '}';

    $data = (array) json_decode($json_string);

    return $data;
}`

and in my controller:

public function update($id = null)
{
    helper('put_helper');

    $data = getPut((array) $this->request->getRawInput()); 

`

I couldn't find any other solution on the web ...

hope it helps someone out ;)

Upvotes: 2

paliz
paliz

Reputation: 353

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);
    }
});

Upvotes: 1

paliz
paliz

Reputation: 353

to get formDAta() use


$this->request->getPost('item');


that works for json or formData request body


$raw=(array)$this->request->getVar()

Upvotes: 0

Related Questions