?
Solved

Passing data from client to server - critical Problem

Posted on 2013-11-02
10
Medium Priority
?
207 Views
Last Modified: 2013-11-10
Hi

I have a business critical problem.  I need to read a folder on a local machine and store the filenames in a MySQL database that is hosted remotely.

I can read the folder with Javascript no problem, but am struggling to work out how to take that information and store it in a MySQL database.  

I'm thinking of storing the filelist as a text file, ftp-ing across to the remote server and then processing it, but it sounds slow and long-winded.  

Currently, I pass the filelist through the URL but this has obvious constraints given the 2K limit, which limits me to ~70 files.

Thanks

Peter
0
Comment
Question by:pambrose1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39619380
zip it !
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39619493
If you are reading the files/filelist through html/js, you should be able to post the data to your server https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest?redirectlocale=en-US&redirectslug=DOM%2FXMLHttpRequest%2FUsing_XMLHttpRequest and use either a webservice or serverside scripting to accept the posted data and update your database.

function reqListener () {
  console.log(this.responseText);
};

var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("get", "yourFile.txt", true);
oReq.send();

Open in new window

or  jquery http://api.jquery.com/jQuery.ajax/
.ajax({
  type: "POST",
  url: "http://mysite.com/some.php",
  data: { name: "John", location: "Boston" }
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
  });

Open in new window

0
 

Author Comment

by:pambrose1
ID: 39619555
Hi!

This looks very promising - thank you!   I am a bit of a novice when it comes to this sort of thing.  

I'm using PHP on the server, so in terms of an integration would this be the pseudo-code?

OnPageLoad
  - Read the file list
  - Construct a string containing the file list
  - Send the file to server (how do I identify the server)

Within the PHP
  - Read the POSTed data

Where is the data POSTed to?  Would this be like a hidden field because normally in PHP I have to read POST data from a screen field?  Any samples would be greatly welcomed

Thanks

Peter
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39619758
You would build your file list in Javascript and then POST that list (array) to a PHP script on your server. The PHP script would read the POST data, looping through each filename and run an INSERT query on the database.

Without seeing how you've built your filelist, this is only an idea of how to do it (it's using jQuery to POST the data to the server. I've hardcoded a ListOfFiles to pass to the server):

var ListOfFiles = new Array('file1.txt', 'file2.txt', 'file3.txt');
$.ajax({
	type: "POST",
	url: "http://www.yourdomain.com/processFiles.php",
	data: {files : ListOfFiles}
})
.done(function(msg) {
	alert(msg);
});

Open in new window

The jQuery code will POST an array of files (ListOfFiles) to a script on your server called processFiles.php. The file list will be available in $_POST['files']. A simple PHP script might then look something like this:

<?php
//connect to your database
$dbh = new PDO("mysql:host=localhost;dbname=yourDB", "username", "password");

//prepare the query
$stmt = $dbh->prepare("INSERT INTO yourTable (filename) VALUES (:filename)");

//bind the :filename parameter to the $file variable
$stmt->bindParam('filename', $file);

//set up a counter
$i = 0;

//loop through the $_POST['files'] data
foreach ($_POST['files'] as $file):
	//run the query
	$stmt->execute();
	//increment the counter
	$i++;
endforeach;

//echo a message back to your jQuery script
echo "Records Inserted: $i";
?>

Open in new window

0
 

Author Comment

by:pambrose1
ID: 39622837
OK .. so we're making progress - I can now post the data in a post field and read it for processing under PHP.

However, I've hit a problem, in that I post the comma separate filelist into a hidden field and read that field into a Page Variable.  When I then try to interrogate that variable, it has converted all the non ASCII codes to %7C for example, instead of a comma.  

I seem to recall that I need to do something with encoding?  Basically, I SUBMIT the form to another PHP page and I think that this is where it gets confused?

Peter
0
 

Author Comment

by:pambrose1
ID: 39622881
I suppose what I'm asking is how to do this with Javascript, rather than JQuery.
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 2000 total points
ID: 39622908
OK. Doing it with javascript will be more difficult - any particular reason why you want to do that?

If you've got the data into a hidden form field, then just use jQuery to POST the form to your php script.

HTML
<form id="files">
<input type="hidden" name="fileList">
<input type="submit" value ="Go">
</form>

Open in new window

jQuery
$('#files').submit(function(e){
	e.preventDefault();
	$.ajax({
		type: "POST",
		url: "www.yourdomain.com/processFiles.php",
		data: $(this).serialize()
	})
	.done(function(msg) {
		alert(msg);
	});
});

Open in new window

PHP Script:
//split the filelist on the comma
$files = explode(",", $_POST['fileList']);

//loop through each file
foreach ($files as $file):
	$file = trim($file);

	//insert $file into your database
endforeach;

Open in new window

0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39622953
>I post the comma separate filelist into a hidden field and read that field into a Page Variable.  When I then try to interrogate that variable, it has converted all the non ASCII codes to %7C for example, instead of a comma.

I think where you are getting confused is when you post the data it is read with commas.  

As an experiment. create a form that does a get instead of a post.  Add multiple form fields with the same name like <input name="test" value="one"><input name="test" value="two">.  Post this to a 2nd php that reads the querystring name as a comma separated file and loop through it. You will see it works as expected.    

This looks like a good answer http:Q_28283934.html#a39622908
0
 

Author Closing Comment

by:pambrose1
ID: 39636549
Such a fast answer!  Brilliant - thank you for taking the time.  In terms of why using javascript, I have this paranoid fear of using external libraries that at some point they will be unsupported!
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39637267
jQuery is here to stay, so embrace it - you won't regret it - there are literally millions of sites that use it!
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question