Katemac Posted December 10, 2012 Share Posted December 10, 2012 Hello, Just need to figure out how to open a pdf from submit button, once the selection is made. Here is the link to the page: http://www.matrix.matrixprovenpos.com/test/index.php I am a very new newbie, but I know it's possible. Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/271836-opening-a-pdf-from-dropdown-menu/ Share on other sites More sharing options...
MDCode Posted December 10, 2012 Share Posted December 10, 2012 You are going to have to be much, much more specific. Especially since the page doesn't load Quote Link to comment https://forums.phpfreaks.com/topic/271836-opening-a-pdf-from-dropdown-menu/#findComment-1398605 Share on other sites More sharing options...
Katemac Posted December 10, 2012 Author Share Posted December 10, 2012 Sorry, I just realized I had it on a private server. It is just a index.php file. I have two separate folders with pdfs. I would like the user to make a choice in the drop down menu. Once they click the pdf, I would like it to open the pdf. I'm sure it is very simple, but I don't even know where to start... Right now I am not even really concerned with the drop down options, I just want the submit button to open the pdf. Does this make sense? Thanks. <html> <head> <title>PHP Test</title> </head> <body> <?php $parent_directory = '.'; $file_types = 'pdf'; //===================================================// // FUNCTION: directoryToArray // // // // Parameters: // // - $root: The directory to process // // - $to_return: f=files, d=directories, b=both // // - $file_types: the extensions of file types to // // to return if files selected // //===================================================// function directoryToArray($root, $to_return='b', $file_types=false) { $array_items = array(); if ($file_types) { $file_types=explode(',',$file_types); } if ($handle = opendir($root)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $add_item = false; $type = (is_dir($root. "/" . $file))?'d':'f'; $name = preg_replace("/\/\//si", "/", $file); if ($type=='d' && ($to_return=='b' || $to_return=='d') ) { $add_item = true; } if ($type=='f' && ($to_return=='b' || $to_return=='f') ) { $ext = end(explode('.',$name)); if ( !$file_types || in_array($ext, $file_types) ) { $add_item = true; } } if ($add_item) { $array_items[] = array ( 'name'=>$name, 'type'=>$type, 'root'=>$root); } } } // End While closedir($handle); } // End If return $array_items; } if (isset($_POST[pickfile])) { // User has selected a file take whatever action you want based // upon the values for folder and file } else { echo ' <html> <head> <script type="text/javascript"> function changeFolder(folder) { document.pickFile.submit(); } </script> </head> <body>'; echo "<form name=\"pickFile\" method=\"POST\">\n"; $directoryList = directoryToArray($parent_directory,'d'); echo "<select name=\"folder\" onchange=\"changeFolder(this.value);\">\n"; foreach ($directoryList as $folder) { $selected = ($_POST[folder]==$folder[name])? 'selected' : ''; echo "<option value=\"$folder[name]\" $selected>$folder[name]</option>\n"; } echo '</select><br><br>'; $working_folder = ($_POST[folder]) ? $_POST[folder] : $directoryList[0][name]; $fileList = directoryToArray($parent_directory.'/'.$working_folder,'f',$file_types); echo "<select name=\"file\">\n"; foreach ($fileList as $file) { echo "<option value=\"$file[name]\">$file[name]</option>\n"; } echo '</select><br><br>'; echo "<button type=\"submit\" name=\"pickfile\">Submit</button>\n"; echo "</form>\n"; echo "</body>\n"; echo "</html>\n"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/271836-opening-a-pdf-from-dropdown-menu/#findComment-1398606 Share on other sites More sharing options...
Christian F. Posted December 11, 2012 Share Posted December 11, 2012 If you want to send the file to the user, then you'll need to look up readfile () and header () in the PHP manual. A search for "force download php" should also give you a lot of examples, and if you add "pdf" to that those search terms you should find some specific cases for exactly what you want to do. Quote Link to comment https://forums.phpfreaks.com/topic/271836-opening-a-pdf-from-dropdown-menu/#findComment-1398703 Share on other sites More sharing options...
Katemac Posted December 12, 2012 Author Share Posted December 12, 2012 Thanks for the reply, but I am not even close to understanding PHP. Quote Link to comment https://forums.phpfreaks.com/topic/271836-opening-a-pdf-from-dropdown-menu/#findComment-1398832 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.