What are common methods for passing data from JavaScript to PHP in a web application?

One common method for passing data from JavaScript to PHP in a web application is by using AJAX requests. This involves sending data asynchronously to a PHP script on the server, which can then process the data and send a response back to the client. Another method is to include the data as part of the URL query string when making a request to a PHP script. This data can then be accessed in the PHP script using the $_GET superglobal array.

// Example of using AJAX to pass data from JavaScript to PHP
// JavaScript code
var data = { name: 'John', age: 30 };
$.ajax({
  type: 'POST',
  url: 'process_data.php',
  data: data,
  success: function(response) {
    console.log('Data processed successfully: ' + response);
  }
});

// PHP code in process_data.php
<?php
$data = json_decode(file_get_contents('php://input'), true);
$name = $data['name'];
$age = $data['age'];
// Process the data as needed
echo 'Data received: ' . $name . ' - ' . $age;
?>