How do I enter 1300 CSV records into a web form?
I've got a fairly extensive data entry task to perform in a PHP application. The application allows you to enter one data set at a time (first name, last name, email), click 'Go', and then uses AJAX to submit the data and blanks the form, putting your cursor back in the first input. I have a data set of 1,300 that needs to be entered using this method. It's currently in an Excel file, but of course that could just as easily be in a database, a CSV, etc.
This is by no means to exploit a site or application, it's to enter user data from an old site into a new one. I'm looking for a way to automate this entry - populate email, populate first name, populate last name, submit the form, delay a second for the response (this is running locally so it should be snappy), select a new row to insert, repeat.
Edit - I absolutely realize that JS isn't the right technology for this job, but I'm confident it can work. The ONLY reason I'm trying to use JS here is because that's the one language capable of getting this done that I'm comfortable with, and doesn't require a ton of work within the application. I can only use other technologies if it's really spelled out.. normally I'd go for the learn开发者_Go百科ing route, but I simply can't this time. Sorry to be so picky!
I think that making a script fill the form and submit it is complicated. The easiest way would be to submit data as the browser does when you press the "submit" button (probably via a POST request).
You said that you would like to do it only in JavaScript. You can use the jQuery ajax features (http://api.jquery.com/jQuery.ajax/) to prepare and send queries.
To prepare the query, you have to use the same parameter names as the form (name attribute of the elements).
The problem is how to use your CSV file from JavaScript. As JavaScript cannot access your file system, you have to use exotic solutions like pasting the CSV text in a textarea. With this method, your javascript can access the CSV data, parse it and prepare requests. Then you can use the ajax functions to send the queries.
On the other hand, you could easily do a small Python script to do this using the urllib module. I think that it would be faster and easier than using JavaScript.
In both cases, the server will act the same as if the request was sent using the "submit" button.
So the steps for your script (any language):
- Get the CSV data
- Parse the data and prepare a request for each line
- Send the HTTP requests
Ok, I worked through this one on my own.. I appreciate all of your input, but I really needed to get this done with JS, as I explained. I'm always very adventurous, but not when I absolutely cannot spend more time than necessary on it (which is rare). The solution isn't complicated:
First I got the data into a MySQL table using LOAD DATA in phpMyAdmin. Trivial. Next, I wrote just a little bit of PHP for the JS to send AJAX requests to.
getUser( $_POST['uid'] );
function getUser( $uid ){
$usr = "********";
$pwd = "********";
$db = "import_users";
$host = "localhost";
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
$SQL = "SELECT * FROM users WHERE id = $uid";
$retid = mysql_db_query($db, $SQL, $cid);
if(!$retid){
echo( "error: " . mysql_error());
} else{
while ($row = mysql_fetch_array($retid)) {
$user = array(
'email' => $row["email"],
'firstname' => $row["firstname"],
'lastname' => $row["lastname"],
'username' => $row["username"],
);
echo json_encode($user);
}
}
}
This PHP does not need to be clean, does not need to sanitize, does not need to be optimized, etc - this is happening ONE TIME, and locally.
Next, the JavaScript:
function incrementUser(){
$.ajax({
type: "POST",
url: "/path-to-php-file/ajax.php",
data: "uid="+i,
success: function(msg){
console.log( "Completed: " + JSON.parse(msg) );
var user = JSON.parse(msg);
jQuery('#email').val(user.email);
//jQuery('#user_login').val(user.username);
jQuery('#first_name').val(user.firstname);
jQuery('#last_name').val(user.lastname);
jQuery('#user_info_form').submit();
}
});
if( i > 1463 ){
clearTimeout( time );
} else{
i = i + 1;
}
}
Again - could be better executed, but it does not matter because this is a one time process. This worked perfectly, and the increment time could probably be even shorter, requiring less time to complete the process.
Thanks again for the input, probably a lot of help for somebody who either has a grasp on these other technologies or some time to play with them. Doesn't go unappreciated!
Sorry if I'm not beign really of help, but if you must pass through the presentation layer to do work with your model, then you have a big problem with the design of your application.
Complaints aside, I'd rather go with a simple script in PHP or Python, than any JS. Using curl
function you can send POST requests to the server with the data parsed from the CSV.
To replicate the request the browser makes. Use a http sniffer like fiddler or paros proxy. Once you see what data the browser sends it should be fairly easy to replicate this behaviour.
An alternative solution is to script the browser with watir. Watir is made to unit test web apps, but can handle this problem fairly easy as well.
精彩评论