I have two page . first page HTML second page PHP .
I want from HTML page send Ajax(jquery)request to PHP page and refresh PHP page.
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<button id="reloadButton">Reload PHP </button>
<script>
$(document).ready(function() {
$("#reloadButton").click(function() {
reloadPhpFunction();
});
function reloadPhpFunction() {
$.ajax({
url: "php_page.php",
type: "POST",
data: { action: "reload" },
success: function(response) {
}
});
}
});
</script>
</body>
</html>
and php_page.php code:
<?php
if (isset($_POST['action']) && $_POST['action'] == 'reload') {
header("Refresh:3");
}
?>
but not working ,please guide me .
I open both pages in two browser tabs, I want when click to Button in html page , php page refresh.
3
Answers
In this way, the PHP page cannot reload as the PHP code has been executed while rendering the page.
You could send updates from server to browser using a tool like Mercure.
Let me ask you what’s the purpose of that code ?
You could prevent that workflow using another application architecture.
Php is a server-side programming language, that means it serves in server and cannot be ‘refreshed’. You should implement you refresh logic with client-side tools like jQuery or Js itself. Anyway if you want to do something with Php (ex. a calculation) in server-side then redirect to last page you can you code bellow:
Or
This document helps you about header function.
This page helps you about the server side and client side of web development
Using
header("Refresh:3");
in the PHP page won’t refresh the HTML page that triggered the Ajax request. This is because the browser interprets this header as a response to the Ajax request, not the original HTML page. To refresh the HTML page, use JavaScript’slocation.reload()
.index.html:
php_page.php: