What is the role of fetch API in sending data from a web page to a PHP script for processing?
The fetch API can be used to send data from a web page to a PHP script for processing by making an HTTP request. This allows the web page to communicate with the server-side PHP script and send data such as form inputs or user actions for processing. The PHP script can then handle the data sent by the fetch API and execute the necessary logic to process it.
<?php
// Retrieve data sent from fetch API
$data = json_decode(file_get_contents("php://input"));
// Process the data as needed
// For example, you can access specific data fields like $data->field1, $data->field2, etc.
// Send a response back to the fetch API
$response = ["message" => "Data processed successfully"];
echo json_encode($response);
?>