-4

What code can I use to make my webpage refresh every time I press enter in PHP?

5 Answers5

3

PHP is a server-side language, the client (the person browsing your website) never has access to it.

You want to use a client-side language, which Javascript would probably be your only option at this moment.

Prime
  • 2,410
  • 1
  • 20
  • 35
2

You can do it using jQuery. Check out this link:

How to detect pressing Enter on keyboard using jQuery?

Which is:

$(document).keypress(function(e) {
    if(e.which == 13) {
        location.reload();
    }
});

Also make sure you include the jQuery library in your heading. Just include the following code in your heading.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
Community
  • 1
  • 1
Arshia
  • 117
  • 9
2

PHP is server side, therefore this will only respond when your browser requests to send him. What you want to do is possible only if you do it from the client side, such as Javascript.

Such a function, Javascript and jQuery, you could help.

$(document).ready(function() {
 $(window).keydown(function(event){
    if(event.keyCode == 13) {
      location.reload();
    }
 });
}
auxler
  • 31
  • 4
0

PHP can not Handle the browser events. But you can do it with AJAX. Follow the following code it may help you:

<html>
<body onkeypress="myFunction()">


  <p>Press a key to refresh the page</p>

<script>
 function myFunction() {
     var xmlhttp = new XMLHttpRequest();
     xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.write( xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET", "refresh.php", true);
    xmlhttp.send();
 }
 </script>

</body>
</html>

refresh.php

<?php
   echo "<script>location.reload();</script>";
?>

You can follow this link to do using AJAX and JS/JQ

Refresh PHP Page using ajax

Community
  • 1
  • 1
Rahul
  • 5,594
  • 7
  • 38
  • 92
0

You can Refresh page using JavaScript, with specific time interval.

code

<script type="text/javascript">
  setTimeout(function(){
    location = ''
  },60000)
</script>
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85