Solved

Passing data from client to server - critical Problem

Posted on 2013-11-02
10
201 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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 82

Expert Comment

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

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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 500 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 52

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

914 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now