Solved

Passing data from client to server - critical Problem

Posted on 2013-11-02
10
203 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dynamic Dropdowns 15 32
Currency in SQL? 2 31
AWK - Blank file output zero bytes why? 18 23
Button on Table, name table1 not working 4 22
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

837 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