将PHP结果放在HTML页面中 [英] Place PHP results inside HTML page

查看:153
本文介绍了将PHP结果放在HTML页面中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP文件,其代码可以回显一些HTML。我想向一些最终用户提供PHP文件,可以这样做:

I have a PHP file which has code to echo some HTML. I would like to provide the PHP file to some end users which can be done like this:

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

不幸的是,我的用户将拥有index.html,但这不起作用。我不想让我的用户将HTML文件更改为PHP。另一种方法是修改.htaccess文件:

Unfortunately my users will for instance have index.html and this will not work. I don't want to ask my users to change there HTML file in to PHP. Another approach is to modify the .htaccess file:

<Files index.html>
AddType application/x-httpd-php .html
</Files>

我也不想问这些问题。那么我的其他选择是什么?是否可以在HTML文件中显示回显的结果?也许在一些Javascript的帮助下?

I don't want to ask this of them as well. So what are my other options? Is it possible to show the echoed results in HTML file? Maybe with the help of some Javascript?

推荐答案

你可以用AJAX做到这一点。它可能看起来有点挑战,但坦率地说比许多人想象的要简单得多。事实上,它非常简单。

You can do this with AJAX. It might look a bit challenging, but it is frankly much simpler than many think. In fact, it's pretty easy.

Ajax包含在您的javascript代码中,如下所示:

Ajax goes in your javascript code, and looks like this:

$('#stSelect').change(function() {
    var sel_stud = $(this).val();
    //alert('You picked: ' + sel_stud);

    $.ajax({
        type: "POST",
        url: "your_php_file.php",
        data: 'theOption=' + sel_stud,
        success: function(whatigot) {
            alert('Server-side response: ' + whatigot);
        } //END success fn
    }); //END $.ajax
}); //END dropdown change event






请注意数据从PHP文件进入您的HTML文档在AJAX调用的成功函数中,必须处理那里。这就是您将接收到的数据插入DOM的位置。


Note that the data from the PHP file comes into your HTML document in the success function of the AJAX call, and must be dealt with there. So that's where you insert the received data into the DOM.

例如,假设您的HTML文档的DIV具有 id =myDiv。要将PHP中的数据插入到HTML文档中,请使用以下代码替换行: alert('服务器端响应:'+ whatigot);

For example, suppose your HTML document has a DIV with the id="myDiv". To insert the data from PHP into the HTML document, replace the line: alert('Server-side response: ' + whatigot); with this:

$('#myDiv').html(whatIgot);

Presto!你的DIV现在包含从PHP文件回显的数据。

Presto! Your DIV now contains the data echoed from the PHP file.

ajax可以通过更改控件的值来触发(如上例所示),或仅在文档加载时:

The ajax can be triggered by a change to an control's value (as in the above example), or just at document load:

$(function() {
//alert('Document is ready');

    $.ajax({
        type: "POST",
        url: "your_php_file.php",
        data: 'Iamsending=' + this_var_val,
        success: function(whatigot) {
            //alert('Server-side response: ' + whatigot);
        } //END success fn
    }); //END $.ajax
}); //END document.ready

查看这个例子关于它是如何工作的想法。

Look at this example for ideas on how it works.

请注意,上面的示例使用jQuery,因此在页面的标记中需要此引用:

Note that the above examples use jQuery, and therefore require this reference in the tags of your page:

<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>

这篇关于将PHP结果放在HTML页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆