Is there a way to simulate a button click in the browser to trigger a POST request in PHP without using JavaScript?
To simulate a button click in the browser to trigger a POST request in PHP without using JavaScript, you can create a form with a hidden input field and submit it using PHP. This can be achieved by setting the form's action attribute to the desired URL and method attribute to POST, then populating the hidden input field with the necessary data before submitting the form programmatically.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Handle the POST request
$data = $_POST['data'];
// Perform necessary actions with the data
echo "POST request triggered with data: " . $data;
} else {
// Simulate button click to trigger POST request
echo "<form id='postForm' method='post' action='http://example.com/post_handler.php'>";
echo "<input type='hidden' name='data' value='example_data'>";
echo "<input type='submit' value='Submit' style='display:none;'>";
echo "</form>";
echo "<script>document.getElementById('postForm').submit();</script>";
}
?>