从html执行php脚本

I'm trying to connect to my SQL server to get some data in a html table. To do this, I have a php script with the necessary code. The issue is to execute the script.

How can I tell html (or javascript) to execute the script?

I can tell you, these methods didn't work for me

Javascript

$.get("testConnection.php");

(I have no idea where to put this next code)

AddType application/x-httpd-php .htm .html

I've read that it's possible to send a request via Ajax, but how can I connect to my server (example: mysql5.test.com) and database (databaseForTesting). Also, I don't have any experience/knowledge in Ajax.

Thanks!

You can use inclue php funcion to include your file into HTML like below

   <html> 
     <title>HTML with PHP</title>
     <body>
     <h1>My Example</h1>

     <?php
     include 'testConnection.php';
     ?>

     <b>Here is some more HTML</b>

     <?php
     //more php code
     ?>

    </body>
   </html>

To use ajax, you must first include jquery in the head of your doc, then put the following code below at the end of the body tag (right before </body> tag)

You can then edit the data variable to send your data as $_POST. To access the data this script sends to your php script, you just call the $_POST variables. Ex: to access var2, put in your php script $_POST['var2']. If you use GET instead of post, remember to use $_GET['var2'] in your php script to get that variable.

var data = 'var1=value&var2=value2&var3=value3';

$.ajax({
            type: "POST", //can be POST or GET
            url: "URL_TO_SCRIPT_GOES_HERE.php",
            dataType: "html", //or json
            data: data, //data to send as $_POST to script
            success: function(response) {

                //Once data received, do this
                alert(response);

            },

            error: function(response) {


            }

        }); 

The database connection line should just be put in the php file. It has nothing to do with the jquery ajax script.

To execute the ajax call, you can just run it how I have here (it will load on page load) or you can put it in a javascript function and call it according to an event:

function my_ajax_call() {
var data = 'var1=value&var2=value2&var3=value3';

$.ajax({
            type: "POST", //can be POST or GET
            url: "URL_TO_SCRIPT_GOES_HERE.php",
            dataType: "html", //or json
            data: data, //data to send as $_POST to script
            success: function(response) {

                //Once data received, do this
                alert(response);

            },

            error: function(response) {


            }

        }); 


}

A onclick button trigger:

<a onclick='my_ajax_call()'>CALL AJAX</a>