Search code examples
phpformsphpword

Using PHPWord to download form results


I've downloaded/installed PHPWord on my site. I understand how PHPWord works based upon the examples.

I just need a little direction on how to create a simple form and use PHPWord to download the result. Specifically, my students will be filling out a 'form' (read: the answers to the homework), and using PHPWord to download it. I know I could just give them a word doc, but I'd like it to be available on the wiki I've built with notes, powerpoints, lab documents, etc. This way, it's open in a tab while they are working through the lab document on the wiki, and they can save an editable copy, not just a PDF or something.

All I really need is a simple example of a question (What color is your hair?), a text area for the student to write it in, and how that answer fits into the addText part of (from the github example):

$section->addText(
    htmlspecialchars(
        '"Learn from yesterday, live for today, hope for tomorrow. '
            . 'The important thing is not to stop questioning." '
            . '(Albert Einstein)'

I understand how to make a simple get/post PHP form. I think this is what I want to do, but after hours of searching, I cannot seem to find a simple example of using a form to replace the hard coded addText in this example.


Solution

  • If you know and understand how to make the simple get/post PHP form then writing the submitted form value with the PhpWord is fairly simple.

    So you have your input text area for the hair color in your page:

    <textarea name="hair_color"></textarea>
    

    And you can access that GET/POST value after form is submitted by:

    $hair_color = $_POST["hair_color"]; // if using POST
    $hair_color = $_GET["hair_color"];  // if using GET
    

    So writing the value to your PhpWord object is just simply:

    // using the previously set variable
    $section->addText(htmlspecialchars($hair_color);
    
    // or using directly the $_POST (or similarly $_GET array)
    $section->addText(htmlspecialchars($_POST["hair_color"]);
    

    If you are after more detailed example - the web is full of examples of how to create a form and process the values with php: just add the PhpWord part into that mix ...