make browse button to load csv data into form

Jun 14, 2008
55
0
66
Hi, I have a csv file exported form excel:

File format is like this:

firstname,lastname,birthdate,city
john,smith,1905.06.07,new york
marry,lay,1985.02.02,birwa
harry,johnson,1936.01.09,moscow

how to make a "load csv" button on my form to load a text file that contains asingle line only like this:

john;smith;1905.06.07;new york

and to fill all the text field boxes on my form?

My form code below:

<?php require_once('Connections/my_MySQL.php'); ?>
<?php
// Load the common classes
require_once('includes/common/KT_common.php');

// Load the tNG classes
require_once('includes/tng/tNG.inc.php');

// Make a transaction dispatcher instance
$tNGs = new tNG_dispatcher("");

// Make unified connection variable
$conn_my_MySQL = new KT_connection($my_MySQL, $database_my_MySQL);

// Start trigger
$formValidation = new tNG_FormValidation();
$tNGs->prepareValidation($formValidation);
// End trigger

// Make an insert transaction instance
$ins_location = new tNG_insert($conn_my_MySQL);
$tNGs->addTransaction($ins_location);
// Register triggers
$ins_location->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Insert1");
$ins_location->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
// Add columns
$ins_location->setTable("location");
$ins_location->addColumn("firstname", "STRING_TYPE", "POST", "firstname");
$ins_location->addColumn("lastname", "STRING_TYPE", "POST", "lastname");
$ins_location->addColumn("birthdate", "DATE_TYPE", "POST", "birthdate");
$ins_location->addColumn("city", "STRING_TYPE", "POST", "city");
$ins_location->setPrimaryKey("id_location", "NUMERIC_TYPE");

// Execute all the registered transactions
$tNGs->executeTransactions();

// Get the transaction recordset
$rslocation = $tNGs->getRecordset("location");
$row_rslocation = mysql_fetch_assoc($rslocation);
$totalRows_rslocation = mysql_num_rows($rslocation);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="includes/skins/mxkollection3.css" rel="stylesheet" type="text/css" media="all" />
<script src="includes/common/js/base.js" type="text/javascript"></script>
<script src="includes/common/js/utility.js" type="text/javascript"></script>
<script src="includes/skins/style.js" type="text/javascript"></script>
<?php echo $tNGs->displayValidationRules();?>
</head>

<body>
<?php
echo $tNGs->getErrorMsg();
?>
<form method="post" id="form1" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>">
<table cellpadding="2" cellspacing="0" class="KT_tngtable">
<tr>
<td class="KT_th"><label for="firstname">Firstname:</label></td>
<td><input type="text" name="firstname" id="firstname" value="<?php echo KT_escapeAttribute($row_rslocation['firstname']); ?>" size="32" />
<?php echo $tNGs->displayFieldHint("firstname");?> <?php echo $tNGs->displayFieldError("location", "firstname"); ?> </td>
</tr>
<tr>
<td class="KT_th"><label for="lastname">Lastname:</label></td>
<td><input type="text" name="lastname" id="lastname" value="<?php echo KT_escapeAttribute($row_rslocation['lastname']); ?>" size="32" />
<?php echo $tNGs->displayFieldHint("lastname");?> <?php echo $tNGs->displayFieldError("location", "lastname"); ?> </td>
</tr>
<tr>
<td class="KT_th"><label for="birthdate">Birthdate:</label></td>
<td><input type="text" name="birthdate" id="birthdate" value="<?php echo KT_formatDate($row_rslocation['birthdate']); ?>" size="32" />
<?php echo $tNGs->displayFieldHint("birthdate");?> <?php echo $tNGs->displayFieldError("location", "birthdate"); ?> </td>
</tr>
<tr>
<td class="KT_th"><label for="city">City:</label></td>
<td><input type="text" name="city" id="city" value="<?php echo KT_escapeAttribute($row_rslocation['city']); ?>" size="32" />
<?php echo $tNGs->displayFieldHint("city");?> <?php echo $tNGs->displayFieldError("location", "city"); ?> </td>
</tr>
<tr class="KT_buttons">
<td colspan="2"><label>
<input type="submit" name="browse" id="browse" value="Load csv" />
</label>
<input type="submit" name="KT_Insert1" id="KT_Insert1" value="Insert record" />
</td>
</tr>
</table>
</form>
<p> </p>
</body>
</html>
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,838
4,817
75
It looks like the first thing you need to do is upload the file to the server. This looks like a good intro to what you can do with a file once you've got it; while this looks like a good function to use in place of fgets.

Once that's done, what's your goal? Do you want to push the entire thing into the database? That could be done with mostly PHP code on the file processing page, with just a notice if it succeeded or failed.

Or are you looking to go through the file line-by-line, populate the form, and validate each line yourself before clicking Insert record? That could probably be done too, more like what you did above, but parsing the data out of a given line of the file first, and redirecting after the submit to the same form, giving the next line.