Excel File Upload With Other Form Data

[Solved] Excel File Upload With Other Form Data | Vb - Code Explorer | yomemimo.com
Question : excel file upload with other form data

Answered by : hassan-aslam

//index.php
function add_words(user_id){ var formdata = new FormData(document.getElementById("word_form")); formdata.append("user",user_id); $.ajax({ url: "../admin/metadata_ajax.php?type=102", type: "POST", data: formdata, cache: false, processData: false, contentType: false, success: function(data){ console.log(data); //alert(data); // if(data == 0){ // alert("Empty"); // }else if(data == 1){ // alert("Added Successfully"); // //location.reload(); // }else if(data == 2){ // alert("Error...!"); // } } }); }
//ajax.php
function importWordsFile()
{ date_default_timezone_set("Asia/Karachi"); $time = date("Y-m-d H:i:s"); global $con_sen; $user = $_POST["user"]; $w_pool_name = $_POST["pool_name"]; $w_lang = $_POST["lang"]; $w_type = $_POST["w_type"]; $w_theme = $_POST["word_theme"]; $allowedExts = array("xlsx","xls","csv"); $temp = explode(".", $_FILES["w_excelFile"]["name"]); $extension = end($temp); $file = $_FILES["w_excelFile"]["name"]; if(!in_array($extension, $allowedExts)) return ['status'=>'error','message'=>'File Type '.$extension.' not allowd']; if ($_FILES["w_excelFile"]["error"] > 0) return ['status'=>'error','message'=>"Return Code: " . $_FILES["excelFile"]["error"]]; $directory = "uploads/"; $filename = time().".".$extension; if(!move_uploaded_file($_FILES["w_excelFile"]["tmp_name"],$directory.$filename)) return ['status'=>'error','message'=>'File Uploading failed in '.$directory.' directory']; include('excel_reader2.php'); include('SpreadsheetReader.php'); //Import any XLS, XLSX, CSV files by using these libraries. if(strtolower($extension) == 'csv') include('SpreadsheetReader_CSV.php'); if(strtolower($extension) == 'xlsx') include('SpreadsheetReader_XLSX.php'); $filePath = $directory.$filename; $htmlMessage = "Excel Sheets Started Initilized"; $StartMem = memory_get_usage(); $errors = []; try{ $Reader = new SpreadsheetReader($filePath); $Sheets = $Reader->Sheets(); $sheetCounts = count($Sheets); $htmlMessage .= 'Total Excel Sheets : '.$sheetCounts.'<br>'; foreach ($Sheets as $Index => $Name) { $htmlMessage .= '*** Sheet '.$Name.' Started ***<br>'; $Reader->ChangeSheet($Index); $i=0; foreach ($Reader as $Row) { $i++; if($i>0) { $word = $Row[0]; if(!empty($word)){ $isAdded = $con_sen->query("INSERT INTO words_list (w_value,w_pool_name,w_language,w_type,w_theme,added_on,added_by) VALUES ('$word','$w_pool_name','$w_lang','$w_type','$w_theme','$time','$user')"); } if(!$isAdded) $errors[] = "Error During Insertion at Row No: ".$i." and Error is : " . $mysqli->error; } } $htmlMessage .= '*** Sheet '.$Name.' Ended ***<br>'; } // $mysqli -> close(); $htmlMessage .= '*** Sheet Imported Successfully ***<br>'; if(!empty($errors)){ $errorsText = implode(' <br> ',$errors); $htmlMessage .=$errorsText; return ['status'=>'waring','message'=>$htmlMessage]; } return ['status'=>'success','message'=>$htmlMessage]; }catch (Exception $e) { return ['status'=>'error','message'=>$e->getMessage()]; }
}

Source : | Last Update : Wed, 07 Sep 22

Answers related to excel file upload with other form data

Code Explorer Popular Question For Vb