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).
Add this to your .htaccess:
AddHandler application/x-httpd-php5 .html .htm
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>
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>
The file extension must be .php, so that the server knows to parse the file.