1

I want to refresh my div with PHP code in div tag but it's don't work. console said:

Uncaught TypeError: $(...).load is not a function

at doRefresh

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>
 
</head>
<body>
<div id="actu">
    <?php include 'process.php'; ?>
</div>
 
<script type="text/javascript">
   $(document).ready(function() {
        function doRefresh(){
           $("#actu").load("process.php");
        }
        $(function() {
            setInterval(doRefresh, 2000);
        });
    });

</script>
</body>
</html>

EDIT

OP has updated code based on answer and comments.

5
  • 1
    You fundamentally mistunderstand how this works. PHP code is executed before the page is sent to the browser. JavaScript code is only executed on the browser. You cannot communicate between JavaScript code and PHP code directly. You need to use something like AJAX to send requests back and forth. Commented Dec 18, 2019 at 17:11
  • 1
    </script></script> < there shouldn't 2 of those. Commented Dec 18, 2019 at 17:11
  • 1
    You're loading two different versions of jquery. That may create a conflict. Remove one of your jquery src lines. Also make sure that process.php is not including any other jquery libraries. Commented Dec 18, 2019 at 17:13
  • 1
    @AdamSvestka Your edit is removing one of the scripts tags. We are not allowed to modify code. I declined the edit. Commented Dec 18, 2019 at 17:13
  • 1
    @SumnerEvans load() is an AJAX shorthand method for jQuery Commented Dec 18, 2019 at 17:15

1 Answer 1

1

Include your code in a document ready handler to ensure the script will run once the page loads:

<script type="text/javascript">
   $( document ).ready(function() {
        function doRefresh(){
           $("#actu").load("process.php");
        }

        setInterval(doRefresh, 5000);

    });
</script>

In addition, remove one of the jQuery library requests as this may cause problems. You also have an extra </script> tag which should be removed.

You do not need the second document handler, but it should not hurt anything. I have removed iy here.

Sign up to request clarification or add additional context in comments.

5 Comments

i've the same error :"Uncaught TypeError: $(...).load is not a function at doRefresh". Div tag contents didn't refresh
Did you remove one of the jQuery libraries?
yes i keep "<script src="ajax.googleapis.com/ajax/libs/jquery/3.4.1/…>" in header
Still not working. I edited my code with corrections
I've edited the code (it will not make a difference, just removed some unnecessary bits). Are you running this on a web server? I . cannot replicate your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.