?
Solved

Passing data from client to server - critical Problem

Posted on 2013-11-02
10
Medium Priority
?
208 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 54

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

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 44

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 54

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 44

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

649 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