在.html文档中使用PHP

时间:2022-10-30 13:53:54

I'm working on a job at work where I need to create a web forum. In my stupidity I decided to use javascript, not knowing beforehand that javascript is a client-side language. I need a way to save the data from the javascript onto the server and then be able to read the data. I tried looking at things like node.js, however I would have reconfigure the entire web server (which isn't mine) in order to do this. The other solution is to use php. Here's the problem: There's a bunch of includes used in the html file that set up the layout of the webpage (i.e. css files, html files, and even a php include). I can't change the name of the index file to index.php because it breaks all of the includes inside of the file. So I need a way to save, say a text file, using javascript and html. If there's a way to, I would like to do something very simple, like include a php file in the html and then call a php function in my javascript code to get the contents of the file into my index.html page. I thought there was a way to call a simple command like this:

我正在从事一项工作,我需要创建一个网络论坛。在我的愚蠢中,我决定使用javascript,事先不知道javascript是一种客户端语言。我需要一种方法将数据从javascript保存到服务器上,然后才能读取数据。我试着看看像node.js这样的东西,但是为了做到这一点,我会重新配置整个网络服务器(不是我的)。另一个解决方案是使用php。这就是问题:在html文件中使用了一堆包含设置网页布局的包括(即css文件,html文件,甚至包括php)。我无法将索引文件的名称更改为index.php,因为它会破坏文件内部的所有包含。所以我需要一种方法来保存,比如一个文本文件,使用javascript和html。如果有办法,我想做一些非常简单的事情,比如在html中包含一个php文件,然后在我的javascript代码中调用php函数,将文件内容放入我的index.html页面。我认为有一种方法可以像这样调用一个简单的命令:

<script>
    var thedata = <?php getData() ?>
</script>

where the php function getData() would return a json encoded string with all of the data in it (handled from a separate php file). Is there any way to do this? Any other suggestions for how to handle data storage on a server without changing my index.html file to index.php?

其中php函数getData()将返回一个json编码的字符串,其中包含所有数据(从单独的php文件处理)。有没有办法做到这一点?关于如何在不将index.html文件更改为index.php的情况下处理服务器上的数据存储的任何其他建议?

Note: I tried accessing the apache httpd.conf file and adding a handler to pre-process .html files as php files, but that doesn't seem to work (nothing as simple as echo 'test' works on the html file).

注意:我尝试访问apache httpd.conf文件并添加一个处理程序来预处理.html文件作为php文件,但这似乎不起作用(没有像echo'test'那样简单的工作在html文件上)。

3 个解决方案

#1


3  

Add this to your .htaccess:

将此添加到.htaccess:

AddHandler application/x-httpd-php5 .html .htm

source

资源

It causes Apache to treat HTML files as files that contain PHP. Be careful not to somehow accidentally use PHP syntax in a regular HTML file, though.

它使Apache将HTML文件视为包含PHP的文件。但是,请注意不要以某种方式在常规HTML文件中意外使用PHP语法。

Remember that you'll still need to use PHP tags to enter PHP mode. This works as expected:

请记住,您仍然需要使用PHP标记进入PHP模式。这按预期工作:

<p>html content ...  <?php echo 'hello, world'; ?></p>

But this will output the the echo command:

但是这将输出echo命令:

<p>echo 'hello, world'</p>

#2


2  

If can make your webserver process your pages thru the mod_php. if you are using apache just add this to your .htaccess

如果可以让您的网络服务器通过mod_php处理您的网页。如果您使用的是apache,只需将其添加到.htaccess即可

AddHandler x-httpd-php .html .htm 
AddHandler php-script .php .html .htm
AddHandler php5-script .php .html .htm
AddType application/x-httpd-php .htm 
AddType application/x-httpd-php .html

I hope you understand the repercussion of doing this. all your pages will be processed like that and the memory/cpu use of your pages will be way greater.

我希望你理解这样做的后果。您的所有页面都将被处理,并且页面的内存/ CPU使用量会更大。

if this is to happen inside one single file, make sure to add it inside a statement.

如果这发生在一个文件中,请确保将其添加到语句中。

and within your example you should add:

在你的例子中你应该添加:

<script>
    var thedata = <?php echo getData(); ?>
</script>

#3


-2  

The file extension must be .php, so that the server knows to parse the file.

文件扩展名必须为.php,以便服务器知道解析文件。

#1


3  

Add this to your .htaccess:

将此添加到.htaccess:

AddHandler application/x-httpd-php5 .html .htm

source

资源

It causes Apache to treat HTML files as files that contain PHP. Be careful not to somehow accidentally use PHP syntax in a regular HTML file, though.

它使Apache将HTML文件视为包含PHP的文件。但是,请注意不要以某种方式在常规HTML文件中意外使用PHP语法。

Remember that you'll still need to use PHP tags to enter PHP mode. This works as expected:

请记住,您仍然需要使用PHP标记进入PHP模式。这按预期工作:

<p>html content ...  <?php echo 'hello, world'; ?></p>

But this will output the the echo command:

但是这将输出echo命令:

<p>echo 'hello, world'</p>

#2


2  

If can make your webserver process your pages thru the mod_php. if you are using apache just add this to your .htaccess

如果可以让您的网络服务器通过mod_php处理您的网页。如果您使用的是apache,只需将其添加到.htaccess即可

AddHandler x-httpd-php .html .htm 
AddHandler php-script .php .html .htm
AddHandler php5-script .php .html .htm
AddType application/x-httpd-php .htm 
AddType application/x-httpd-php .html

I hope you understand the repercussion of doing this. all your pages will be processed like that and the memory/cpu use of your pages will be way greater.

我希望你理解这样做的后果。您的所有页面都将被处理,并且页面的内存/ CPU使用量会更大。

if this is to happen inside one single file, make sure to add it inside a statement.

如果这发生在一个文件中,请确保将其添加到语句中。

and within your example you should add:

在你的例子中你应该添加:

<script>
    var thedata = <?php echo getData(); ?>
</script>

#3


-2  

The file extension must be .php, so that the server knows to parse the file.

文件扩展名必须为.php,以便服务器知道解析文件。