3

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?

3
  • 1
    Where are the commas? Commented Mar 7, 2017 at 20:20
  • @HugoWoesthuis I've tried placing the script in the <head> tag. I'll add my php file Commented Mar 7, 2017 at 20:20
  • See the answer of deepakkedia. Commented Mar 7, 2017 at 20:21

2 Answers 2

3

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

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

1 Comment

@JoeSeph79 don't forget to mark the question as a answer ;). Welcome btw
0

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');
   });
 });

Comments

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.