Sunday 15 April 2012

PHP drop down list that downloads a file from a folder when clicked on -



PHP drop down list that downloads a file from a folder when clicked on -

i'm trying create php drop downwards list downloads file folder on server when clicked on. i'm using iis , using joomla plugin sourcerer allows php scripts run on joomla webpages.

i'm using code sample (from stack exchange post not created me) , edited work own environment.

<p> <select name="s1"> <option value="" selected="selected">select document</option> <?php foreach(glob(dirname(path-to-root-dir) . '\path-to-sub-folder\*') $filename){ $filename = basename($filename); echo "<option value='" . $filename . "'>".$filename."</option>"; } ?> </select> </p>

but haven't figured out way create when user clicks illustration test.txt drop downwards list browser prompts them download selected file. i've seen done in .asp i'm new php i'm not sure if possible?

this have less php , more javascript. event of selecting item in select element handled javascript.

for example, if uniquely identify element id:

<select name="s1" id="s1">

then can reliably element in javascript:

var selectelement = document.getelementbyid('s1');

now can bind event handler it:

selectelement.addeventlistener('change', function () { // handle event here });

it looks want handle event getting value of select , directing browser value url. should started:

selectelement.addeventlistener('change', function () { var fileurl = selectelement.options[selectelement.selectedindex].value; window.location.href = fileurl; });

this assumes value right url file. if isn't, you'll need address of course. point responding event , directing browser "download file" client-side operation in javascript.

php

No comments:

Post a Comment