MrLarkins Posted August 18, 2009 Share Posted August 18, 2009 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 More sharing options...
thebadbad Posted August 18, 2009 Share Posted August 18, 2009 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 More sharing options...
MrLarkins Posted August 18, 2009 Author Share Posted August 18, 2009 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 More sharing options...
MrLarkins Posted August 18, 2009 Author Share Posted August 18, 2009 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 More sharing options...
thebadbad Posted August 18, 2009 Share Posted August 18, 2009 Can you view the PDF (or other PDFs) directly in your browser? If not, you're simply missing a plugin. Link to comment https://forums.phpfreaks.com/topic/170904-form-to-select-file/#findComment-901439 Share on other sites More sharing options...
MrLarkins Posted August 18, 2009 Author Share Posted August 18, 2009 it seems it is a Adobe Acrobat Reader 9 problem...i can't load any pdf's in my browser... :'( but that is not this forums problem...thanks for the help on the script...it is working! Link to comment https://forums.phpfreaks.com/topic/170904-form-to-select-file/#findComment-901449 Share on other sites More sharing options...
thebadbad Posted August 18, 2009 Share Posted August 18, 2009 You're welcome. Should be pretty easy to get your browser to display PDFs. I think the Firefox plugin I'm using simply got installed alongside the actual program. Link to comment https://forums.phpfreaks.com/topic/170904-form-to-select-file/#findComment-901453 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.