FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Javascipt killin me!

 


moejoe
Hey, I got this Script:

Code:
function Upload() {
   
   var ex = document.getElementById('ext').value
    extArray = ex.split(", ");
     var file = document.getElementById('file').value
    allowSubmit = false;
    if (!file) return false;
    while (file.indexOf("\\") != -1)
    file = file.slice(file.indexOf("\\") + 1);
    ext = file.slice(file.indexOf(".")).toLowerCase();
    for (var i = 0; i < extArray.length; i++) {
    if (extArray[i] == ext) { allowSubmit = true; }
    }
   
    if (allowSubmit == true) {
    return true;
    document.getElementById('uploadbut').disabled = 'true'
    document.getElementById('uploadbut').value = 'Uploading..Please wait'
    }
    else {
     alert("Please only upload files that end in types:  "
    + (extArray.join("  ")) + "\nPlease select a new "
    + "file to upload and submit again.");
    return false; }
   
 }


It basically checks the extensions via jScript and alerts the user. If its all good, it submits the form and away goes the upload.

Zoom in on this script:
Code:
 if (allowSubmit == true) {
    return true;
    document.getElementById('uploadbut').disabled = 'true'
    document.getElementById('uploadbut').value = 'Uploading..Please wait'
    }


When the return true; is on the top, it submits the form and it uploads away =)
But it doesnt do the other 2 lines below it.

But if i switch the code around like so:
Code:
 if (allowSubmit == true) {
    document.getElementById('uploadbut').disabled = 'true'
    document.getElementById('uploadbut').value = 'Uploading..Please wait'
    return true;
    }


It will change the uploadbutton (uploadbut) to the upload sign and disable it.
But then it doesn't return it as true!?
The weird thing is that it Submits the form, but it doesnt upload the file? I'm guessing my PHP doesn't register that it was a POST function without it being 'true'.

Any help would be awesome.

Thanks.
MrBlueSky
The problem (I think) is this: by disabling 'uploadbut' here the parameter 'uploadbut' isn't send to your PHP script:

Code:

 if (allowSubmit == true) {
    document.getElementById('uploadbut').disabled = 'true'  // Trouble strikes?
    document.getElementById('uploadbut').value = 'Uploading..Please wait'
    return true;
    }


If your form looks like this (example):

Code:


<FORM method='get' action='upload.php'>
  <INPUT type='submit' name='uploadme' value='something' onclick='Upload()'>
  <INPUT type='text' name='test' value='yes'>
</FORM>



The URL your PHP script receives is "http://yoursite.com/upload.php?test=yes" and not "http://yoursite.com/upload.php?uploadme=something&test=yes"! This means that if your PHP scripts does something like this:

Code:

if ($_GET['uploadme']) {
  // handle the uploading
}


the file will not be uploaded and handled correctly. The same goes for using POST instead of GET. So you should check the PHP script and fix this. Or post it here if you need help with it.


Last edited by MrBlueSky on Sun Sep 02, 2007 7:06 pm; edited 2 times in total
alalex
maybe im wrong, but I was thinking that you may have to put a ';' after those lines..?
make it be like this:
Code:
if (allowSubmit == true) {
    document.getElementById('uploadbut').disabled = 'true';
    document.getElementById('uploadbut').value = 'Uploading..Please wait';
    return true;
}


its my try..! Wink
MrBlueSky
alalex wrote:
maybe im wrong, but I was thinking that you may have to put a ';' after those lines..?
make it be like this:


Semicolons are not required in Javascript when you use newlines to seperate your statements, so this isn't a problem. Wink
moejoe
Yeh, the ';' dont matter.

I got it to work.

I added another hidden name to make the php pick up the upload process.

Thanks for the tips!
moejoe
ERRH!

Just tested it, and found that it doesnt work on IE6..

I hate IE..
moejoe
haha,

Got it to work.

Just added a simple script

Code:
document.all.form.submit()


=)
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.