Etable/formType/file
出自六年制學程
目錄
(一)製作表單元素類別
formsome.php 中
class XoopsFormUploadFile extends XoopsFormElement { function XoopsFormUploadFile($caption, $name){ // 提示、後傳變數名 $this->setCaption($caption); $this->setName($name); } function render(){ return "<input type='file' name='".$this->getName()."' id='".$this->getName()."'".$this->getExtra()." />".$this->getExtra('2'); } } class XoopsFormFile extends XoopsFormElementTray{ function XoopsFormFile($caption, $name, $value='', $paths=array('.'=>'當前路徑')){ $this->XoopsFormElementTray($caption,' '); $pathSelect=new XoopsFormSelect('路徑:',$name.'[path]',$value='');// 產生選單物件並指定預選值 $pathSelect->addOptionArray($paths); $this->addElement($pathSelect);// 將選單物件放入表單 $fileUpload=new XoopsFormUploadFile('請選檔:', $name.'[fileName]'); $this->addElement($fileUpload);// 將選單物件放入表單 } }
(二)addElements($i,$value)
加表單元素類型為 file 則$j=new XoopsFormFile($this->fields[$i]['colalias'],'f['.$this->fields[$i]['as_name'].']['.$this->fields[$i]['colname'].']',str_replace("'",''',str_replace("&",'&',$value)),$this->formType["$lc_name"][1]['paths']); $this->form->addElement($j,(($this->fields[$i]['formValidate']==0)?'0':'1')); $this->form->addElement(new XoopsFormHidden('uploadFileField',$lc_name,0)); $this->form->addElement(new XoopsFormHidden('uploadFilePath',$value,0));
(三)新增 login.php 中可以後送檔案的 javascript 函式 postFormData(…)
function postFormData(formID,dataSource,divID){ // 以 fetch 發POST請求,可以上傳檔案,等於原來的 sendFormData+postData var formData = new FormData(); for(var i=0;i<document.getElementById(formID).elements.length;i++){ if(document.getElementById(formID).elements[i].type=='checkbox' || document.getElementById(formID).elements[i].type=='radio'){ if(document.getElementById(formID).elements[i].type=='checkbox'){ // 處理核取方塊,checked回傳值,沒checked回傳空字串 if(document.getElementById(formID).elements[i].checked==true){formData.append(document.getElementById(formID).elements[i].name,document.getElementById(formID).elements[i].value);} else{formData.append(document.getElementById(formID).elements[i].name,'');} }else{ // 處理按鈕,checked回傳值,否則跳過 if(document.getElementById(formID).elements[i].checked==true){formData.append(document.getElementById(formID).elements[i].name,document.getElementById(formID).elements[i].value);} } } else if(document.getElementById(formID).elements[i].type=='hidden'){formData.append(document.getElementById(formID).elements[i].name,document.getElementById(formID).elements[i].value);} //新增檔案資料 else if(document.getElementById(formID).elements[i].type=='file'){formData.append(document.getElementById(formID).elements[i].name,document.getElementById(formID).elements[i].files[0]);} else{formData.append(document.getElementById(formID).elements[i].name,document.getElementById(formID).elements[i].value);} } fetch(dataSource, {method:'POST',body:formData}).then((response)=>{return response.text();}).then((responseText)=>{document.getElementById(divID).innerHTML=responseText;onAjax(a);}); }
(四)修改 etable.php 中的 javascript 函式 checkFormlist(…)
if($field['formType'][0]=='file'){$str.="\t\telse if(!(/".$field['formValidate'][0]."/.test(document.getElementsByName('f[".$field['as_name']."][".$field['colname']."][path]')[0].value) && (/".$field['formValidate'][0]."/.test(document.getElementsByName('f[".$field['as_name']."][".$field['colname']."][fileName]')[0].value)))){alert(\"".$field['formValidate'][1]."\");}\n";}
(五)改 etable.php 中 fieldValue2sqlStr 函式
(六)main() / 單筆資料前置處理 / 編畢、刪一筆
「$action=='ins_ed'」或「$action=='inss'」:已透過 fieldValue2sqlStr 函式處理
「$action=='erase'」
if($action=='erase'){ foreach($this->editables['erase'] as $as_name){ foreach($this->formType as $k=>$v){ if($v[0]=='file'){ unlink(mysqli_result(DB::queryF("select `$k` from ".$this->tables["$as_name"]['t_name']." where 1 ".$this->decode($this->rid["$as_name"])." limit 1"),0,0)); } } DB::queryF("delete from ".$this->tables["$as_name"]['t_name']." where 1 ".$this->decode($this->rid["$as_name"])." limit 1"); // by jj now } }
「$action=='edited'」:已透過 fieldValue2sqlStr 函式處理
(七)改 colValue()
elseif($this->tran){ … elseif($this->formType["$lc_name"][0]=='file'){ if($value){$value="<img src='".substr($value,strlen($this->formType[$lc_name][1]['docsPath']))."' width=100 height=* />";} else{$value="";} } … }