What is the primary role of PHP in an AJAX application?
B
To generate JavaScript code
C
To process data sent via AJAX and return a response
Analysis & Theory
PHP runs on the **server side** and handles data processing, returning responses to the AJAX call.
Which JavaScript object is commonly used to send AJAX requests to a PHP script?
Analysis & Theory
`XMLHttpRequest` is the built-in JavaScript object used to **send AJAX requests**.
How do you send form data to a PHP file using AJAX POST?
A
xhr.send('name=John&age=25')
C
xhr.request({name: 'John'})
Analysis & Theory
Data can be sent to a PHP script via POST by calling `xhr.send()` with form-encoded data.
What PHP superglobal is used to access POST data from an AJAX call?
Analysis & Theory
`$_POST` is used in PHP to access **form data sent with the POST method.**
Which PHP function sends text or JSON back to the browser in an AJAX response?
Analysis & Theory
`echo` is used in PHP to **output content**, which can then be read by JavaScript in the AJAX response.
What must you set in PHP to send a JSON response?
A
header('Content-Type: text/html')
B
header('Content-Type: text/plain')
C
header('Content-Type: application/json')
D
header('Content-Type: text/xml')
Analysis & Theory
To return JSON correctly, use `header('Content-Type: application/json')` before sending data.
Which PHP function is used to convert a PHP array into JSON format?
Analysis & Theory
`json_encode()` converts a PHP array or object to a JSON string.
How do you read an AJAX response in JavaScript?
Analysis & Theory
`xhr.responseText` contains the **response body** as a plain string from the PHP server.
Which PHP file extension should you use when handling an AJAX request?
Analysis & Theory
PHP code must be placed in files with the `.php` extension to be interpreted by the server.
Why is using AJAX with PHP useful in modern web development?
A
It makes JavaScript faster
B
It eliminates the need for CSS
C
It allows background server communication without reloading the page
D
It hides PHP from users
Analysis & Theory
AJAX allows web pages to **interact with PHP scripts** in the background, enabling dynamic updates.