0

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:

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

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

Josh Herr
  • 81
  • 1
  • 2
  • You should be able to add .html as a file extension for PHP to handle. It's possible however, that maybe farther up the file .html is already being assigned to text/html. I'd check to see if .html occurs anywhere else in your httpd.conf file first. – Joshua Burns Feb 06 '13 at 00:05
  • possible duplicate of [How do I add PHP code to .html files?](http://stackoverflow.com/questions/11312316/how-do-i-add-php-code-to-html-files) – John Conde Feb 06 '13 at 00:05
  • Off-topic: Be sure to *echo* output the result of getData() using json_encode(), like this `var thedata = ;` You don't have to add quotes, they will be added by json_encode() – thaJeztah Feb 06 '13 at 00:07
  • It might be better to use Ajax to query your server as a webservice? (then you add to write your server accordingly, in PHP if you want) – H_I Feb 09 '13 at 14:53

3 Answers3

4

Add this to your .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.

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

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

But this will output the the echo command:

<p>echo 'hello, world'</p>
Waleed Khan
  • 11,426
  • 6
  • 39
  • 70
2

If can make your webserver process your pages thru the mod_php. if you are using apache just add this to your .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.

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>
Danilo Kobold
  • 2,562
  • 1
  • 21
  • 31
  • Thanks for all of your help! Oddly though, those two AddType lines changed all of the webpages so it popped up with a screen asking to save the webpage, and then you couldn't load it. Not sure why it does that... I believe it's the "application" in "application/x-httpd-php". When I take it out it no longer breaks the webpages. – Josh Herr Feb 07 '13 at 04:37
-2

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

zeyorama
  • 444
  • 3
  • 12