0

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>';
Adam Hinx
  • 215
  • 3
  • 21
  • 1
    I don't think you can create a word document only by adding ".doc" at the end. – XCS Nov 26 '12 at 18:57
  • Create a real Word document. What you have is not valid. – Brad Nov 26 '12 at 18:58
  • Ive tested the above code, without the hyperlink and it works fine. Brad - Could you expand. – Adam Hinx Nov 26 '12 at 19:00
  • You're aware that JS is on the client and that PHP is on the server, but you still expect it to work? And you also expect your string to magically become a word document? You need to understand the wall of separation between server and client and design accordingly – Ruan Mendes Nov 26 '12 at 19:02

4 Answers4

2

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:

$(document).ready(function()
{

    $('a').click(function()

    {
    $('#result').load('createWordDoc.php');

    });

});

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.

Community
  • 1
  • 1
jpsnow72
  • 965
  • 2
  • 15
  • 45
0

try this http://www.phpdocx.com/, it is a php library which can create word documents. But it is more complicated than you are trying there.

dmgig
  • 4,400
  • 5
  • 36
  • 47
  • Cheers. I have looked into this library, but as you've guessed from my original code - I'm trying to keep it as simple as possible. – Adam Hinx Nov 26 '12 at 19:01
0

Your calling a PHP function from a JavaScript "onclick" event. Without an ajax call I don't believe this is possible. The PHP function never reaches the client side user because it is a server side function. After the server parses all the PHP code, the page is sent to the user and all PHP functions, variables, and references are discarded (except for $_SESSION of course). What you have cannot work without an ajax call to the server.

War10ck
  • 12,387
  • 7
  • 41
  • 54
0

This link explains which headers you need to output to create a Word doc: http://webcheatsheet.com/php/create_word_excel_csv_files_with_php.php

but also, as you've built the page, that php runs (well, doesn't) before the site is built, so really, your button should trigger an AJAX call to your server to then generate the doc

Robot Woods
  • 5,677
  • 2
  • 21
  • 30