My aim: Create a button (or hyperlink) which generates a word document on click.
Below is the code I have created, but it’s not working. No errors shown. I’m aware PHP is a server side scripting language, so I think it has something to do with that.
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function createReport() {
$fp = fopen("report.doc", 'w+');
$str = "This is the text for the word file created through php programming";
fwrite($fp, $str);
fclose($fp);
return false;
}
</script>
Hyperlink:
echo '<a href="#" onclick="createReport();">Generate</a>';
In your createReport() function, you are going to need some AJAX which calls some php code. On this php page, you can do something like this: Create Word Document using PHP in Linux
Edit:
Your js should looks something like this… I am using jQuery:
createWordDoc.php would then contain your php code which creates the word document. You can also have this page echo something. WHatever you echo will be displayed in the div with id=result.