jQuery $.ajax request of dataType json will not retrieve data from PHP script
When debugging AJAX interactions between JavaScript and PHP, seeing [object Object] in an alert or a blank screen is a classic symptom of a mismatched content type negotiation. In the scenario described by Piotr, the client-side code explicitly requests dataType: "json", while the server-side PHP script uses echo json_encode(). At first glance, this seems perfectly configured. However, the failure to populate the input fields indicates that the browser is not interpreting the response as a parseable JSON object, but rather as a string containing one.
The root cause lies in how HTTP headers dictate how the client processes the payload. When PHP executes echo json_encode($json_arr); without wrapping it in header('Content-Type: application/json');, the server sends the raw JSON string back with a default content type, typically text/html. Modern browsers and the jQuery library are strict about this. If the response does not strictly adhere to the application/json MIME type, jQuery will fail to parse the data into an object. Instead, it receives a string representation of the object. When you try to access properties like response.type on that un-parsed string, JavaScript treats it as a generic object wrapper, resulting in the [object Object] error when attempting to use the value in DOM selectors.
To resolve this, the PHP script must explicitly set the correct header before any output is echoed. The fix requires modifying the start of contents/php_scripts/admin_scripts.php:
<?php
// Set the content type header immediately
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *'); // Optional, if running on different ports/domains
if( isset( $_POST[ "get_member" ] ) )
{
$member_id = $_POST[ "get_member" ];
$query = "select * from members where id = '$member_id'";
// Security Note: The original code uses mysql_query with direct variable interpolation.
// This is highly vulnerable to SQL injection attacks. In a production environment,
// you must use prepared statements (PDO or MySQLi) instead of string concatenation.
$result = mysql_query($query);
if (!$result) {
die(json_encode(['error' => 'Database query failed']));
}
$row = mysql_fetch_array($result);
// Proceed with data extraction...
$type = $row[ "type" ];
$name = $row[ "name" ];
// ... rest of variables
$json_arr = array(
"type" => $type,
"name" => $name,
"fname" => $fname,
"lname" => $lname,
"email" => $email,
"phone" => $phone,
"website" => $website,
"image" => $image
);
// This echo is now safe because the header was set
echo json_encode( $json_arr );
} else {
// Always define a response or handle empty cases gracefully
echo json_encode(['error' => 'No member ID provided']);
}
On the client side, while dataType: "json" is helpful, it is not a substitute for correct server headers. If you strictly want to ensure the data is treated as JSON regardless of the header (though this is less recommended), you can remove the dataType setting and handle parsing manually using JSON.parse(). However, relying on dataType is generally preferred for cleaner code, provided the server responds correctly.
Additionally, there are significant architectural considerations here. The PHP script relies on the deprecated mysql_* functions, which have been removed in modern PHP versions. Migrating to PDO or MySQLi with prepared statements is mandatory for security and compatibility. Furthermore, while the original approach of fetching an entire row into a JSON array works, returning a specific error structure (like ['error' => '...']) when the member ID is missing or invalid prevents the JavaScript from crashing silently.
If you are starting a new project or refactoring legacy code, consider using a framework like Laravel. It handles JSON responses, database queries, and routing out of the box, reducing the likelihood of header mismatches. You can explore further resources at https://laravelcompany.com to see how modern frameworks streamline these specific AJAX patterns.
Finally, ensure that the Access-Control-Allow-Origin header is set if the AJAX call originates from a different domain or subdomain than the PHP script, although local development usually bypasses this strict CORS enforcement.
