0

I have created a HTML form and a Word document. Actually I want to update the specific fields in the Word document. Like the following, user will fill in this form

<form action="test.php" method="post">
    <table>
       <tr>
        <th>Organization / Company: 
           <td>
              <input type="text" name="company" size="50" required>
           </td>
        </th>
       </tr>
       <tr>
        <th>Telephone: 
           <td>
              <input type="text" name="tele" size="50" required>
           </td>
        </th>
       </tr>
       <tr>
        <th>E-Mail:
           <td>
              <input type="email" name="email" size="50" required>
        </th>
       </tr>
   </table>
   <input id="submitBtn" type="submit" value="Submit">
</form>

Then save these data to the fields (highlighted in red) in the Word document: enter image description here

Is there any advice? Thank you so much!

Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
Calvin
  • 13
  • 1
  • 6

2 Answers2

0

Check out: https://github.com/PHPOffice/PHPWord

What you need to do is create a word template-file where you have placeholders for your values. For example, in Word the organization field should have ${organization} as its value (instead of "xxxxxx").

require_once '../PHPWord.php';

// TODO: Get values from form here.
$org = ...
$email = ...
$phone = ...

$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('template.docx');

$document->setValue('organization', $org);
$document->setValue('phone', $phone);
$document->setValue('e-mail', $email);

$document->save('customer.docx');
Mani
  • 1,597
  • 15
  • 19
0

I saw the answer to a similar question here Generating word documents with PHP

Assuming you have obtained the values from the form (via $_POST or $_GET) and stored them in $company , $telephone, $email respectively, then you can do something like this:

<?php
  header("Content-type: application/vnd.ms-word");
  header("Content-Disposition: attachment;Filename=document_name.doc");    
  echo "<html>";
  echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
  echo "<body>";
  echo "<b>created by PHP!</b>";
  echo "Organization / Company: $company <br>";
  echo "Telephone: $telephone <br>";
  echo "E-Mail: $email <br>";
  echo "</body>";
  echo "</html>";
  exit;
?>

But you have to re-direct the user to the page containing the above code before you set anything in the HTML body, otherwise, the code wont work. Modifiying the header should be the first thing to do.

Ahmad
  • 12,336
  • 6
  • 48
  • 88