473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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

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
7 3294
Dormilich
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
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

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

Similar topics

3
by: pei_world | last post by:
hi I am new to C# programming. can anyone tell me what is the standard way to store high sensitive user data for application, so that application next run can get back those data.
5
by: mac | last post by:
thanks, mac
4
by: Piotrekk | last post by:
Hi everyone. I would like to ask about good habid, of keeping program data. I have written in C# Server and Client applications, and now i need to care about authorization. What i mean is that...
5
by: Seok Bee | last post by:
Dear Experts, I currently trying to use the FileUpload control from asp.net 2.0 to upload files. The uploading of the file I would like to store it in the Access Database. Unfortunately, I've no...
2
by: Learner | last post by:
Hello, I am trying to store the data entered in a webform in the database. I have few Int and one SmallDateTime filed in my table in SQL Server 2005 database. I have made a storedproc to store the...
3
by: noridotjabi | last post by:
Say I'm writting a program. In this program for some reason I need to store data somewere were I will be able to access it again. I don't want to store it in a file because then it could be...
0
by: jitendar | last post by:
Hii all, Anyone tell me "How to parse html file and store its contents in sql server 2005 in asp.net C#" Thanks and regards Jitendar talwar Mailto: jiten.talwar007@gmail.com
2
by: Alex | last post by:
Hey Guys.. I'm just now starting to learn VB 2005, but I have a question that might help me in the long run. I hope to write applications which store user data, and without wanting the user to...
3
by: Chuck Beck | last post by:
I am struggling with this issue right now. I have read in a number of places that you should not store the image itself in the database, but rather only the filepath/filename. I can't seem to...
9
by: Sanitprof | last post by:
hi there, how to upload an excel file and its data to be stored into database(phpmyadmin) using php. thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.