PDA

View Full Version : Javascript Functions - forms



rockdoc
Mar 26th, 2007, 12:38 PM
Hi,
I have a 3 functions I need to associate with a "delete file" button click. The name of the file is in a hidden form field called "file". The form is called "form1". I'm not interested in actually deleting the file from the server.

The functions need to...

1) Check to see whether the filename exists in the hidden field
2) If it exists, give the user a confirm delete option
3) If OK, remove the file name from the hidden field


The functions, are listed individually below, I'm just having trouble combining them all together.

// check if file exists

<script>
function myfunction() {

if (document.form1.file.value.length > 0)

// This is where the confirm delete would fit in ????

else {
alert('There are no files attached!');
}
}
</SCRIPT>


/////Confirm delete
<script>
function confirmPopup( message )
{
var bConfirm = confirm( message );
return bConfirm;
}
</script>


///Delete name from hidden field

this.form.file.value=' '


///The button with the confirmation message (having trouble fitting the other 2 functions into this)

<input type="button" value="Delete File" onClick="return confirmPopup('Are you sure you want to delete this file?');">

nikkiH
Mar 26th, 2007, 03:06 PM
Are you trying to delete a file from the user's computer, or the server?
If the name of the file is in the hidden field, do you have the full path? If not, how do you know where it is?
If the file is on the server, you need AJAX to talk to the server from the client.
If the file is on the client, you need special permissions to be deleting files, or even to "look" at the client file structure. These permissions are rare for internet sites, so I will assume if this is a client file, this is either a desktop HTA type app or an intranet app using the FileSystem object (ActiveX).

rockdoc
Mar 26th, 2007, 03:18 PM
I actually know the full path to the file on the server.

I'm just interested in removing the filename (e.g. filename.doc) from the hidden field.

nikkiH
Mar 26th, 2007, 08:02 PM
Ok, a couple more questions before I post too much code:

Why return in a button? Were you trying to catch that to prevent form submission? If not, there is little reason for it.
Did you need those function separated the way you have them? There is a more logical way, IMO.

The basic way I would imagine your goals being satisfied (assuming that return was so the form was not submitted) would be to make the button a submit button, put the return code in the onsubmit of the form, and use one function (may call other functions, of course) that returns the boolean true/false.

Pseudo-codeish (not necessarily syntactically correct, just to give idea, obviously could be made even more concise and should have error checking, etc):

form ... onsubmit="return confirmDeleteOk(this)";



function checkFileExists(filename)
{
//call AJAX to check file on server; returns true/false

return YourAjaxCall(filename);
}

function confirmDeleteOk(frm)
{
if (checkFileExists(frm.hiddenElementName.value))
{
if (confirm("Are you sure you want to delete?"))
{
// put any delete code here, if that's what you wanted
frm.hiddenElementName.value = '';
return true;
}
else return false;
}
else
{
alert("File doesn't exist.");
return false;
}
}

rockdoc
Mar 27th, 2007, 04:04 AM
Thankyou very much nikkiH,
Yes, I needed your more logical way of putting the functions together.

I can run with that now.

I'll let you know how I ended up.

(BTW, I use a button as part of a file management tool separate from the form submission)

rockdoc
Mar 28th, 2007, 09:51 AM
Ok,
here is what I finally finished with...it's functioning the way I wanted.

Thanks again for your help nikkiH




function myfunction() {

if (document.form1.file.value.length > 0)
{
var answer = confirm('Are you sure you want to delete this file [ ' + document.form1.file.value + ' ] ?')
if (answer){
//server side deletion process (e.g AJAX) here

alert('Your file [ ' + document.form1.file.value + ' ] has now been deleted.')
document.form1.file.value=''
}

}
else {
alert('There are no files attached to do delete!');

}
}

nikkiH
Mar 28th, 2007, 07:36 PM
Great!
One little thing -- you may want to trim the value for whitespace, just in case the user accidentally hits a space or something. Just checking length won't catch that.