Jump to content

Form to Select file


MrLarkins

Recommended Posts

Hi,

 

I am a math teacher.  I have a server and I have made available all the practice worksheets on my site.  There are a lot of them.  I thought it would save coding if the worksheet was called using a php script. 

 

Here is the form

  <form action="getworksheet.php" method="post">
  Chapter: 
  	<select name="chapter">
  		<option value="">--</option>
  		<option value="01">1</option>
  		<option value="02">2</option>
  		<option value="03">3</option>
  		<option value="04">4</option>
  		<option value="05">5</option>
  		<option value="06">6</option>
  		<option value="07">7</option>
  		<option value="08">8</option>
  		<option value="09">9</option>
  		<option value="10">10 </option>
  		<option value="11">11</option>
  		<option value="12">12</option>
  	</select>
  Section: 
  	<select name="section">
  		<option value="">--</option>
  		<option value="01">1</option>
  		<option value="02">2</option>
  		<option value="03">3</option>
  		<option value="04">4</option>
  		<option value="05">5</option>
  		<option value="06">6</option>
  		<option value="07">7</option>
  		<option value="08">8</option>
  	</select>
  Worksheet Type:
  	<select name="type">
  		<option value="">--</option>
  		<option value="PR">Practice</option>
  		<option value="RE">Reteaching</option>
  		<option value="EN">Enrichment</option>
  	</select>
  <p> 												
  <input type="submit" value="Fetch It!" />
</form>

The worksheets come named in the following standard form: A10204PR.pdf where 'A1' for algebra1, '02' for chapter 2, '04' for section 4, and 'PR' for practice.  Lower case pdf.

I'd like the pdf file to load into the browser on submit.  Can I get some help starting.  I'm under the impression that this one will be short and sweet.  Also, the pdf files are in the same directory as the form page

Link to comment
https://forums.phpfreaks.com/topic/170904-form-to-select-file/
Share on other sites

Short and sweet:

 

getworksheet.php

<?php
$file = 'A1' . $_POST['chapter'] . $_POST['section'] . $_POST['type'] . '.pdf';
if (file_exists($file)) {
header("Location: $file");
}
?>

 

But; we should add some more checks, to make sure the POST values are legit. We can also hide the actual filename, if that suits you:

 

getworksheet.php

<?php
$file = 'A1' . $_POST['chapter'] . $_POST['section'] . $_POST['type'] . '.pdf';
//check if chapter and section are in the allowed ranges, if the type is allowed, and if the resulting file exists
if (
in_array((int) $_POST['chapter'], range(1, 12)) &&
in_array((int) $_POST['section'], range(1, ) &&
in_array($_POST['type'], array('PR', 'RE', 'EN')) &&
file_exists($file)
) {
//output file with proper header
header('Content-type: application/pdf');
readfile($file);
} else {
//redirect to the form page
header('Location: filename_of_form_page.html');
}
?>

 

Actually didn't mean to write the whole thing for you, it's just that I'm pretty efficient here at night. Lol ;)

Link to comment
https://forums.phpfreaks.com/topic/170904-form-to-select-file/#findComment-901415
Share on other sites

LoL, you are too funny.  I was going to ask several questions about things like "what if the combination of selections didn't actually match a file?" and "how do i connect the chosen options to make a single file name?"

 

but from your post, those are both answered. Thanks.  I'll try it out.

Link to comment
https://forums.phpfreaks.com/topic/170904-form-to-select-file/#findComment-901417
Share on other sites

ok, seems like its working...kinda.  I am getting a popup that says

The Adobe Acrobat/Reader that is running can not be used to view PDF files in a Web Browser.  Adobe Acrobat/Reader version 8 or 9 is required.  Please exit and try again.

and i have the latest v9 installed.

 

Ideas?

Link to comment
https://forums.phpfreaks.com/topic/170904-form-to-select-file/#findComment-901433
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.