Is it possible to display the output of a PHP script on an HTML page without embedding the entire PHP code within the HTML file?
To display the output of a PHP script on an HTML page without embedding the entire PHP code within the HTML file, you can use AJAX to make a request to the PHP script and then dynamically update the HTML content with the response. This allows you to keep your PHP logic separate from your HTML markup, making your code more organized and easier to maintain.
// PHP script (example.php)
<?php
// Your PHP logic here
echo "Hello, world!";
?>
// HTML file
<!DOCTYPE html>
<html>
<head>
<title>Display PHP Output</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div id="output"></div>
<script>
$(document).ready(function() {
$.ajax({
url: 'example.php',
type: 'GET',
success: function(response) {
$('#output').html(response);
}
});
});
</script>
</body>
</html>
Related Questions
- What are potential pitfalls of relying on third-party PHP scripts for website development?
- In what scenarios would it be more beneficial to use an external cronjob service compared to setting up a cronjob on your own machine for PHP tasks?
- What are the basic steps for connecting to a database in PHP?