I have an external Javascript file initialize_database.js that uses JQuery to call a PHP script to create a database and some tables. I've tested my PHP script by adding some HTML to it to run it on its own, and it works fine. My HTML is as follows:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<title>Test Webpage</title>
</head>
<body>
<script src="initialize_database.js"></script>
<div>
<h2>Nothing here yet!</h2>
</div>
</body>
</html>
Here is initialize_database.js:
$(document).ready(function() {
$.get('testPhp.php' {
alert('Databases were initialized');
});
});
I'd like to have the Javascript run as soon as the page loads so that the database can be created right away. All files are in the same directory. What am I doing wrong?
-
1Where are the commas?Hugo Woesthuis– Hugo Woesthuis2017年03月07日 20:20:17 +00:00Commented Mar 7, 2017 at 20:20
-
@HugoWoesthuis I've tried placing the script in the <head> tag. I'll add my php fileJoeSeph79– JoeSeph792017年03月07日 20:20:49 +00:00Commented Mar 7, 2017 at 20:20
-
See the answer of deepakkedia.Hugo Woesthuis– Hugo Woesthuis2017年03月07日 20:21:44 +00:00Commented Mar 7, 2017 at 20:21
2 Answers 2
Comma and function initialization is required after the URL for executing the function correctly.
$(document).ready(function() {
$.get('testPhp.php', function() {
alert('Databases were initialized');
});
});
Check this
1 Comment
you can use this code . if you want to run when dom ready then
$(document).ready(function() {
$.get('testPhp.php', function() {
alert('Databases were initialized');
});
});
if you want to run this full load then use bellow
$( window ).load(function() {
$.get('testPhp.php', function() {
alert('Databases were initialized');
});
});