簡體   English   中英

從HTML執行php腳本

[英]Execute php script from html

我正在嘗試連接到我的SQL Server以在html表中獲取一些數據。 為此,我有一個帶有必要代碼的php腳本。 問題是執行腳本。

如何告訴html(或javascript)執行腳本?

我可以告訴你,這些方法對我不起作用

使用Javascript

$.get("testConnection.php");

(我不知道將下一個代碼放在哪里)

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

我讀過可以通過Ajax發送請求,但是如何連接到服務器(例如:mysql5.test.com)和數據庫(databaseForTesting)。 另外,我對Ajax沒有任何經驗/知識。

謝謝!

要使用ajax,您必須首先在文檔的開頭添加jquery,然后將以下代碼放在body標記的末尾( </body>標記之前)

然后,您可以編輯data變量,以$ _POST的形式發送數據。 要訪問此腳本發送到您的php腳本的數據,只需調用$ _POST變量。 例如:要訪問var2,請輸入您的php腳本$ _POST ['var2']。 如果您使用GET而不是post,請記住在PHP腳本中使用$ _GET ['var2']來獲取該變量。

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) {


            }

        }); 

數據庫連接線只應放在php文件中。 它與jquery ajax腳本無關。

要執行ajax調用,您可以按照此處的方式運行它(它將在頁面加載時加載),也可以將其放在javascript函數中並根據事件進行調用:

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) {


            }

        }); 


}

一個onclick按鈕觸發器:

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

您可以使用inclue php函數將文件include到HTML中,如下所示

   <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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM