views:

248

answers:

1

I am trying to build a downloads section where the visitor completes a form and then selects check boxes to open download dialogs.

I would like the form to submit and the dialogs to open, when the button is clicked.

I am not very good at writing scripts, although I can read them. Currently I can only get one or the other to work!

The form is a standard PHP form....

This is the javascript I am using

var suffix=1;
function downloadAll(oFrm){
var oChk = oFrm.elements["file"+(suffix++)];
if (oChk){
if (oChk.checked){
location.href = oChk.value;
setTimeout(function(){downloadAll(oFrm)}, 2000);
}
else{
downloadAll(oFrm);
}
}
}

This is an abbreviated part of my HTML

<input type="checkbox" name="file1" id="file1" value="/download.php?f=blank.gif" /><label for="file1">blank.gif</label>
<input type="checkbox" name="file2" id="file2" value="/download.php?f=atlas_blue_hover2.png" /><label for="file2">atlas_blue_hover2.png</label>
<input type="checkbox" name="file3" id="file3" value="/download.php?f=Tabs.css" /><label for="file3">Tabs.css</label>
<input type="button" value="Download All" onClick="suffix=1;downloadAll(this.form);return false;" />

I think that this might not be working because the onclick cancels out post in a PHP form

method="post" action="http://url"

Can I do similar to above using onSubmit? Or using the checkbox values in PHP?

Any help would be appreciated.

+1  A: 

Don't return false from your onclick handler. That cancels the default action

Keith Rousseau
Thank you Keith but this has not worked.I have tried return true, and getting rid of the return. Both, stops the javascript from working but allows the PHP form to post its action!
Jessica
Main the button type Submit instead of Button also.
Nissan Fan
Thank you for your suggestion Nissan Man I have tried<input value="Submit" type="submit" onClick="suffix=1;downloadAll(this.form); return false;" /> opens javascript and <input value="Submit" type="submit" onClick="suffix=1;downloadAll(this.form);" /> posts the form actionI still can't do both...any suggestions appreciated
Jessica
Return true; as he noted in his answer.
Nissan Fan
Return True also still posts the form action without activating the javascript
Jessica
What exactly is downloadAll doing? Is it just opening a new window? As soon as the post to the server occurs, the entire page will be refreshed.
Keith Rousseau
Yes it opens a download dialog, Thanks
Jessica
Looking at the downloadAll function, you don't ever popup a new window for the download. You set location.href, which just redirects the current page. If you want a download dialog, you will need to use window.open
Keith Rousseau