简体   繁体   English

从HTML执行php脚本

[英]Execute php script from html

I'm trying to connect to my SQL server to get some data in a html table. 我正在尝试连接到我的SQL Server以在html表中获取一些数据。 To do this, I have a php script with the necessary code. 为此,我有一个带有必要代码的php脚本。 The issue is to execute the script. 问题是执行脚本。

How can I tell html (or javascript) to execute the script? 如何告诉html(或javascript)执行脚本?

I can tell you, these methods didn't work for me 我可以告诉你,这些方法对我不起作用

Javascript 使用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). 我读过可以通过Ajax发送请求,但是如何连接到服务器(例如:mysql5.test.com)和数据库(databaseForTesting)。 Also, I don't have any experience/knowledge in Ajax. 另外,我对Ajax没有任何经验/知识。

Thanks! 谢谢!

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) 要使用ajax,您必须首先在文档的开头添加jquery,然后将以下代码放在body标记的末尾( </body>标记之前)

You can then edit the data variable to send your data as $_POST. 然后,您可以编辑data变量,以$ _POST的形式发送数据。 To access the data this script sends to your php script, you just call the $_POST variables. 要访问此脚本发送到您的php脚本的数据,只需调用$ _POST变量。 Ex: to access var2, put in your php script $_POST['var2']. 例如:要访问var2,请输入您的php脚本$ _POST ['var2']。 If you use GET instead of post, remember to use $_GET['var2'] in your php script to get that variable. 如果您使用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) {


            }

        }); 

The database connection line should just be put in the php file. 数据库连接线只应放在php文件中。 It has nothing to do with the jquery ajax script. 它与jquery ajax脚本无关。

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: 要执行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) {


            }

        }); 


}

A onclick button trigger: 一个onclick按钮触发器:

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

You can use inclue php funcion to include your file into HTML like below 您可以使用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