By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,983 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,983 IT Pros & Developers. It's quick & easy.

How to parse CSV file and store it's data into database?

P: 4
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 :
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function storeQueEmail(){
  3. <?php    
  4.     $file = $_FILES[csv][tmp_name]; 
  5.     $handle = fopen($file,"r"); 
  6.  
  7.     //loop through the csv file and insert into database 
  8.     do { 
  9.         if ($data[0]) { 
  10.             $record['contact_first'] = $data[0];
  11.             $record['contact_last'] = $data[1];
  12.             $record['contact_email'] = $data[2];
  13.             $record['subject'] = $_REQUEST['subject'];
  14.             $record['message'] = $_REQUEST['message'];
  15.             $record['status'] = 0;
  16.  
  17.             $oAdminEmail->insertQueEmail($record);
  18.             } 
  19.     } while ($data = fgetcsv($handle,1000,",","'")); 
  20.  
  21.     ?>  
  22.     }
  23. </script>
This is HTML code :
Expand|Select|Wrap|Line Numbers
  1. <html xmlns="http://www.w3.org/1999/xhtml"> 
  2. <head> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
  4. <title>Import a CSV File with PHP & MySQL</title> 
  5. </head> 
  6. <body> 
  7. <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1" > 
  8.   Subject : <br/>
  9.   <input type="text" name="subject" id="subject" required/> <br/>
  10.   Choose your upload type: <br /> 
  11.     <input name="csv" type="file" id="csv" accept=".csv" required/> <br/>
  12.   Content : <br/>
  13.   <textarea name="message" cols="50" rows="10" required></textarea><br/>
  14.   <input type="submit" name="submit" value="Submit" onclick="storeQueEmail()"/> 
  15. </form> 
  16. </body> 
  17. </html> 
Mar 28 '14 #1
Share this Question
Share on Google+
7 Replies


Dormilich
Expert Mod 5K+
P: 8,639
Unfortunately, I am using php code inside javascript function which is not efficient enough.
that would be because PHP is a server-side language and JavaScript is a client-side language. just look at the webpage’s source code and you will see what your JavaScript function really contains.
Mar 28 '14 #2

P: 4
what can i do next? is it possible to provide me a sample which use javascript to store csv data into database?
Mar 28 '14 #3

Dormilich
Expert Mod 5K+
P: 8,639
is it possible to provide me a sample which use javascript to store csv data into database?
that would require JavaScript to have direct access to your DB, which is imossible for client-side JavaScript. what you have to do is let JavaScript collect the data send it over to a server script and let the server script insert the data into the DB.
Mar 28 '14 #4

P: 4
that's mean I cannot use javascript to get data from CSV file? beside javascript, can i use other language to get CSV data?
Mar 28 '14 #5

Dormilich
Expert Mod 5K+
P: 8,639
you can use JavaScript, but you must not confuse what is to happen on the server and what on the client.
Mar 28 '14 #6

P: 4
okay. i understand what my problem but can you provide me a sample code on how to read csv file by using javascript instead of using php? i don't want to pass my code to another new php and i just want to validate and do everything inside one page.
Mar 31 '14 #7

Dormilich
Expert Mod 5K+
P: 8,639
ok, given you’d read your CSV with JavaScript, it still isn’t capable of saving the data in the DB.

i don't want to pass my code to another new php
the include command might be useful.
Mar 31 '14 #8

Post your reply

Sign in to post your reply or Sign up for a free account.