PHP ver gevorderde |
|
Ik heb een AJAX uploader, die word door php gegenereerd:
<?php
function showFileUploader($uploaderId) {
if (in_array($uploaderId, $this->uploaderIdArray)) {
die($uploaderId." already used. please choose another id.");
return '';
}
else {
$this->uploaderIdArray[] = $uploaderId;
return 'Upload foto<br />
<form id="formName'.$uploaderId.'" method="post" enctype="multipart/form-data" action="'.__abUrl.'/system/upload/1/imageupload.php?dirname='.$this->uploadDirectory.'" target="iframe'.$uploaderId.'">
<input type="hidden" name="id" value="'.$uploaderId.'" />
<span id="uploader'.$uploaderId.'" style="font-family:verdana;font-size:10;">
Upload File: <input name="'.$uploaderId.'" type="file" value="'.$uploaderId.'" onchange=\'return uploadFile(this,"'.$this->uploadDirectory.'")\' /></span>
<span id="loading'.$uploaderId.'"></span>
<iframe name="iframe'.$uploaderId.'" src="imageupload.php" width="400" height="100" style="display:none"> </iframe>
</form>';
}
<?php function showFileUploader($uploaderId) { if (in_array($uploaderId, $this->uploaderIdArray)) { die($uploaderId." already used. please choose another id."); return ''; } else { $this->uploaderIdArray[] = $uploaderId; return 'Upload foto<br /> <form id="formName'.$uploaderId.'" method="post" enctype="multipart/form-data" action="'.__abUrl.'/system/upload/1/imageupload.php?dirname='.$this->uploadDirectory.'" target="iframe'.$uploaderId.'"> <input type="hidden" name="id" value="'.$uploaderId.'" /> <span id="uploader'.$uploaderId.'" style="font-family:verdana;font-size:10;"> Upload File: <input name="'.$uploaderId.'" type="file" value="'.$uploaderId.'" onchange=\'return uploadFile(this,"'.$this->uploadDirectory.'")\' /></span> <span id="loading'.$uploaderId.'"></span> <iframe name="iframe'.$uploaderId.'" src="imageupload.php" width="400" height="100" style="display:none"> </iframe> </form>'; }
=> werkt
volgende stap: kies een bestand, onchange word er een functie uitgevoerd:
html
<form id="formNameid1" method="post" enctype="multipart/form-data" action="http://localhost/pj_muller/1//system/upload/1/imageupload.php?dirname=houses/" target="iframeid1">
<input type="hidden" name="id" value="id1" />
<span id="uploaderid1" style="font-family:verdana;font-size:10;">
Upload File: <input name="id1" type="file" value="id1" onchange='return uploadFile(this,"houses/")' /></span>
<span id="loadingid1"></span>
<iframe name="iframeid1" src="imageupload.php" width="400" height="100" style="display:none"> </iframe>
</form>
<form id="formNameid1" method="post" enctype="multipart/form-data" action="http://localhost/pj_muller/1//system/upload/1/imageupload.php?dirname=houses/" target="iframeid1"> <input type="hidden" name="id" value="id1" /> <span id="uploaderid1" style="font-family:verdana;font-size:10;"> Upload File: <input name ="id1" type ="file" value ="id1" onchange ='return uploadFile(this,"houses/")' /></span > <span id="loadingid1"></span> <iframe name="iframeid1" src="imageupload.php" width="400" height="100" style="display:none"> </iframe> </form>
en de js functie die dus word opgeroepen:
function uploadFile(obj, dname) {
uploadDir=obj.value;
idname=obj.name;
dirname=dname;
filename=uploadDir.substr(uploadDir.lastIndexOf('\\')+1);
document.getElementById('loading'+idname).innerHTML="<img src='system/upload/1/loading.gif' alt='loading...' />";
uploaderId = 'uploader'+obj.name;
uploader = obj.name;
document.getElementById('formName'+obj.name).submit();
timeInterval=setInterval("traceUpload()", 1500);
}
function uploadFile(obj, dname) { uploadDir=obj.value; idname=obj.name; filename =uploadDir .substr(uploadDir .lastIndexOf ('\\')+1); document.getElementById('loading'+idname).innerHTML="<img src='system/upload/1/loading.gif' alt='loading...' />"; uploaderId = 'uploader'+obj.name; uploader = obj.name; document.getElementById('formName'+obj.name).submit(); timeInterval=setInterval("traceUpload()", 1500); }
dit werkt als ik het in een testbestand zet, maar als ik het in een totaal geheel zet krijg ik de volgende error:
Citaat: document.formNameid1 has no properties
[Break on this error] document.formNameid1.submit();
(FireBug)
waarom werkt dit niet?
|