简体   繁体   English

使用HTML中的外部Javascript文件运行PHP脚本

[英]Run PHP Script using an external Javascript file in HTML

I have an external Javascript file initialize_database.js that uses JQuery to call a PHP script to create a database and some tables. 我有一个外部Javascript文件initialize_database.js ,它使用JQuery调用PHP脚本来创建数据库和一些表。 I've tested my PHP script by adding some HTML to it to run it on its own, and it works fine. 我已经测试了我的PHP脚本,通过添加一些HTML来自己运行它,它运行正常。 My HTML is as follows: 我的HTML如下:

<!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 : 这是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. 我想在页面加载后立即运行Javascript,以便立即创建数据库。 All files are in the same directory. 所有文件都在同一目录中。 What am I doing wrong? 我究竟做错了什么?

Comma and function initialization is required after the URL for executing the function correctly. 在正确执行函数的URL之后需要逗号和函数初始化。

$(document).ready(function() {
    $.get('testPhp.php', function() {
        alert('Databases were initialized');
    });
});

Check this 检查一下

you can use this code . 你可以使用这段代码。 if you want to run when dom ready then 如果你想在dom准备就绪时运行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM