I trying to store CSV data to database using onclick function. Unfortunately, I am using php code inside javascript function which is not efficient enough. Therefore, I hope that I can get any suggest or solution to improve efficiency of my project by using javascript instead of php to store CSV data into database.
This is javascript with php code :
This is HTML code :
This is javascript with php code :
Code:
<script> function storeQueEmail(){ <?php $file = $_FILES[csv][tmp_name]; $handle = fopen($file,"r"); //loop through the csv file and insert into database do { if ($data[0]) { $record['contact_first'] = $data[0]; $record['contact_last'] = $data[1]; $record['contact_email'] = $data[2]; $record['subject'] = $_REQUEST['subject']; $record['message'] = $_REQUEST['message']; $record['status'] = 0; $oAdminEmail->insertQueEmail($record); } } while ($data = fgetcsv($handle,1000,",","'")); ?> } </script>
Code:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Import a CSV File with PHP & MySQL</title> </head> <body> <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1" > Subject : <br/> <input type="text" name="subject" id="subject" required/> <br/> Choose your upload type: <br /> <input name="csv" type="file" id="csv" accept=".csv" required/> <br/> Content : <br/> <textarea name="message" cols="50" rows="10" required></textarea><br/> <input type="submit" name="submit" value="Submit" onclick="storeQueEmail()"/> </form> </body> </html>
Comment