<div id="pdf">
<script type="text/javascript">
function get_pdf_file( elem ) {
var filename = elem.options[elem.selectedIndex].value;
var file_id = elem.options[elem.selectedIndex].id;
var id = elem.options[elem.selectedIndex].index;
// output some stuff to console: it works
console.log( 'get_pdf_file( elem ) filename id: ' + id );
}
</script>
<div id="choose_pdf">
<form id="select_pdf_file">
<select name="pdf_file" onChange="get_pdf_file( this )">
<?php
for ( $i = 0; $i < $count_filenames; $i++ ) {
if ( $i == 0 ) {
echo '<option id="file_' . $i . '" value="' . $filenames[$i] . '" selected >' . $filenames[$i] . '</option>';
$pdf_file = $filenames[$i];
} else {
echo '<option id="file_' . $i . '" value="' . $filenames[$i] . '">' . $filenames[$i] . '</option>';
}
}
# output from above:
#<option id="file_1" value="tutorial_1.pdf" selected >tutorial_1.pdf</option>
#<option id="file_2" value="tutorial_2.pdf" >tutorial_2.pdf</option>
# ...
# ...
?>
</select>
</form>
</div>
<!-- here is the container to output the selected file "$pdf_file" (see above)-->
<object width="1000" height="803" type="application/pdf" data="<?php echo $pdf_file ?>?#zoom=58.7&scrollbar=1&toolbar=1&navpanes=1" id="pdf_content">
<p>Insert your error message here, if the PDF cannot be displayed.</p>
</object>
</div>
The variable $count_filenames
is the number of elements in an array named $filenames
containing the names of various .pdf files...
Believe I miss some code as user action selecting tutorial_2.pdf for example always output the same file ie tutorial_1.pdf…
Please help solving this!
you should do the same $pdf_file = $filenames[$i];
in else condition too.
else {
echo '<option id="file_' . $i . '" value="' . $filenames[$i] . '">' . $filenames[$i] . '</option>';
$pdf_file = $filenames[$i];
}
Even better,
for ( $i = 0; $i < $count_filenames; $i++ ) {
if ( $i == 0 )
echo '<option id="file_' . $i . '" value="' . $filenames[$i] . '" selected >' . $filenames[$i] . '</option>';
else
echo '<option id="file_' . $i . '" value="' . $filenames[$i] . '">' . $filenames[$i] . '</option>';
$pdf_file = $filenames[$i];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.