Solved

Passing data from client to server - critical Problem

Posted on 2013-11-02
10
200 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
Comment Utility
zip it !
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
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
Comment Utility
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 42

Expert Comment

by:Chris Stanyon
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:pambrose1
Comment Utility
I suppose what I'm asking is how to do this with Javascript, rather than JQuery.
0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
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
Comment Utility
>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
Comment Utility
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 42

Expert Comment

by:Chris Stanyon
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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 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…

772 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

12 Experts available now in Live!

Get 1:1 Help Now