skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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:

    header('Location: http://www.example.com/'); // In Php, it actually redirects not reload
    

    Or

    location.reload(); // in Js
    

    This document helps you about header function.

    This page helps you about the server side and client side of web development

    Login or Signup to reply.
  3. 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’s location.reload().

    index.html:

    <script>
        $(document).ready(function() {
            $("#reloadButton").click(function() {
                reloadPhpFunction();
            });
            function reloadPhpFunction() {
                $.ajax({
                    url: "php_page.php",
                    type: "POST",
                    data: { action: "reload" },
                    success: function(response) {
                        // Reload the current page after the Ajax request is successful
                        location.reload();
                    }
                });
            }
        });
    </script>
    

    php_page.php:

    <?php
    if (isset($_POST['action']) && $_POST['action'] == 'reload') {
        echo "Reloaded successfully";
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search