Reputation: 2046
In our office we have forms that we create for work that we do and the idea is to simplify and speed up the process of creating these forms. I have up to 4 different Word documents to create (.docx format) and a form to create them. Based of the user's input (checkboxes, text field), other data is queried from the MySQL database I have to populate the bookmarks of the documents. A lot of the information for the documents is duplicated so I'm trying to keep the database queries to a minimum and reuse the same code/variables, if possible. I have the document creation scripts working individually and I can get these documents to prompt the user to download them if only one of the checkboxes is selected. But I'm having difficulty creating the code to prompt the user to download more than one of the documents at a time.
I've tried doing a single submit button on the form with the checkboxes, but it only ever prompts for one download regardless of whether one or all of the checkboxes are checked. I've tried doing a javascript open in new window, but then the data doesn't get imported into the document correctly due to the open xml format of the .docx extension.
I've read that doing multiple download of files from one request isn't possible due to security reasons. But the way I have each of these form creation scripts, there shouldn't be just one request. I'm newer to HTTPRequests, so I likely don't understand it correctly.
This is the structure that I have for my document creation:
index.php (includes form for selecting which documents to create and work order number of job) -> results.php (which takes input from form and queries database for addtional information that is common to all documents, then routes information to document creation scripts) -> toform1.php and/or toform2.php and/or toform3.php and/or toform4.php (depending on what checkboxes were clicked). The form scripts do additional queries to the database as needed to complete population of the documents. Each document has a different layout and has some different bookmarks.
Here's the code for index.php:
<form action="results.php" id='forms' method='post'>
<table id='formTable'>
<tr>
<td><label for="workOrderNum">Work Order Num:</label></td>
<td><input type="text" id='workOrderNum' name='workOrderNum'> </input></td>
</tr>
<tr>
<td><label for="formSelection">Forms to Generate: </label></td>
<td>
<input type="checkbox" name='form1' id='form1'/>
<label for="form1"><span></span>Form 1</label><br />
<input type="checkbox" name='form2' id='form2'/>
<label for="form2"><span></span>Form 2</label><br />
<input type="checkbox" name='form3' id='form3'/>
<label for="form3"><span></span>Form 3</label><br />
<input type="checkbox" name='form4' id='form4'/>
<label for="form4"><span></span>Form 4</label><br />
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" id='generateForm' value='Generate Form(s)' class='button'></input></td>
</tr>
</table>
</form>
Here's a portion of the code for results.php:
$workOrderNum = htmlentities($_POST['workOrderNum']);
...
if(isset($_POST['form1']) && $_POST['form1']){
include 'toform1.php';
}
if(isset($_POST['form2']) && $_POST['form2']){
include 'toform2.php';
}
if(isset($_POST['form3']) && $_POST['form3']){
include 'toform3.php';
}
if(isset($_POST['form4']) && $_POST['form4']){
include 'toform4.php';
}
And here's the common parts of the form scripts (toform1.php, toform2.php, etc.):
$word = new COM("word.application");
// Hide MS Word application window
$word->Visible = 0;
$template_file = 'C:/xampp/htdocs/portal/includes/templates/form1.docx';
//file name is different based on initiating form.
//Create new document
$word->Documents->Open($template_file);
//WO Number
$woNumBookMark = 'WONum';
$objBookmark = $word->ActiveDocument->Bookmarks($woNumBookMark);
$range = $objBookmark->Range;
$range->Text = htmlentities($workOrderNum) . '/' . htmlentities($lotID);
$filename = tempnam(sys_get_temp_dir(), "word");
$word->Documents[1]->SaveAs($filename);
// Close and quit
$word->Quit();
unset($word);
header("Content-Length: ".filesize($filename));
//header("Content-Type: application/force-download");
header("Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-Disposition: attachment;Filename=document_name.docx");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
// Send file to browser
readfile($filename);
unlink($filename);
Again, the toform1.php (etc.) scripts work fine when called individually, but when I try to implement something that calls more than one of these at a time, only one document is created and only one is being prompted for download.
I understand that I may be completely off my rocker in my expectations, but the idea is to keep it simple for the user. Putting the files into a ZIP file and having the users save, then unzip then open is no easier than our current method of doing things. That and I don't even have the first clue as to how to combine all of these into a ZIP file for download since they're created with different scripts at different times.
I'm looking for help because this is the last hurdle to implementing this in our office which will be a huge time saver, so I'm really looking for help to finish this project. So correction to current code or pointing me in the right direction to accomplish this would be awesome! Thanks in advance for your help!
Upvotes: 2
Views: 1887
Reputation: 2046
To help anyone else out there who may be struggling with this. It took a while to find this coding nugget, but it met the needs I had. All I had to do to create several downloads at the same time with the code above was put this: target='_blank'
in the opening <form>
tag. So now it reads:
<form action="results.php" id='forms' method='post' target='_blank'>
All I have to say is, "Wow". I spent hours looking for solutions to this issue, and all it ended up being was that attribute. Thank heavens to whoever put that in their code so I would eventually find it.
Upvotes: 1