How does Codeigniter receive the ajax post data in controller

How does Codeigniter receive the ajax post data in controller

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Converting CodeIgniter POST Arrays to Objects

In CodeIgniter, handling AJAX requests often feels straightforward until you hit the specific need for object-oriented data access. You might be sending JSON payloads via jQuery or vanilla JS and receiving an associative array in your controller method, as shown here:

public function login() {
    $data = $this->input->post();
    // now I can get account and passwd by array index
    $account = $data["account"];
    $passwd = $data["passwd"];
}

This approach works perfectly fine. However, if you prefer accessing properties like $data->account instead of $data['account'], you need to convert that input array into an object. This is a common pattern when you want to use get() or isset() directly on the data structure without remembering whether you're dealing with a map or a class instance.

In CodeIgniter, there isn't a built-in method in the $this->input class to automatically cast the post array into an object. The framework keeps things simple by default. To achieve this conversion, you have two primary options depending on your specific version of the framework and your tolerance for third-party helpers.

Using json_decode with JSON_THROW_ON_ERROR

The most straightforward way to convert a PHP associative array into an object is using the built-in json_encode and json_decode functions. Since your input data is already in an array, you simply encode it back to a JSON string and then decode it with the OBJ flag.

Here is how you can modify your controller:

public function login() {
    // Get raw post data as an array
    $postData = $this->input->post();

    // Convert the array to an object
    $dataObject = json_decode(json_encode($postData), true) === null ? $postData : json_decode(json_encode($postData));

    // Or simply force it if you know the structure is safe:
    $data = (object)$postData; 
}

Wait, that second line is actually the most direct and readable way. PHP allows type casting from an array directly to an object using (object). This creates a standard stdClass object where each key in the array becomes a property.

public function login() {
    $data = (object)$this->input->post();
    
    // Now you can access properties like this:
    $account = $data->account;
    $passwd = $data->passwd;
}

This approach is version-agnostic and doesn't rely on any specific CodeIgniter helper. It works in CI 2.x, 3.x, and even modern iterations of the framework. The trade-off is minimal: you are creating a new object instance rather than reusing the internal array representation, but for input validation logic, this distinction rarely impacts performance or functionality.

Handling Null Values Gracefully

A common pitfall when casting arrays to objects is how PHP handles null values within the data. If your JSON payload contains a field with a null value, the resulting object will have a property set to null. This is generally expected behavior and shouldn't break your logic unless you are checking for existence without verifying the type first.

For example:

if (!empty($data->account)) {
    // Logic runs only if account exists and is not empty string or null
}

If you cast a multidimensional array to an object, nested arrays become properties that contain array values rather than objects, unless the JSON structure strictly mirrors the desired object hierarchy. In this specific login scenario, a flat associative array mapping to a single-level object is exactly what you need.

Why Not Use stdClass Directly?

You might wonder why we don't just instantiate a custom class like class LoginData { public $account; public $passwd; }. While possible, this adds unnecessary complexity for simple AJAX endpoints. CodeIgniter's superglobal handling is designed to be flexible; forcing a specific domain model onto raw input data before validation often leads to fragile code if the JSON payload changes slightly.

Using (object) cast preserves the dynamic nature of the input while giving you the syntax sugar of property access. It keeps your controller clean without requiring you to define schema classes for every single form submission.

Final Note on Framework Migration

If you are currently using CodeIgniter but planning a migration to Laravel, note that the underlying mechanism for accessing POST data changes entirely. In Laravel, you would use $request->json()->user() or $request->account directly, often utilizing request attributes instead of casting arrays to objects manually. Resources like https://laravelcompany.com offer detailed guides on this transition if your project evolves beyond the CodeIgniter ecosystem.

Our network