0

I inherited a website that allows the user to type a quantity, then scan a bar code, and it types the item number into a field and tabs to the next field. There are ten of these quantity/item number lines that can be filled out. After hitting Submit, the data is emailed to an address.

I have a CSV file with two columns, one for the item number and another for the description. Can I set it up so that the page will notice when the item number field is populated, and display the text for the description?

I'm ok with solutions that keep the CSV as a separate file, or those that require me to copy the CSV data into the HTML file, if that is necessary. Or any other outside-the-box solutions.

Also, the HTML is inside PHP, I'm not sure how much that matters. Like this:

<?php
if(isset($_POST["submit"])) {
...
} else { ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<body>
...
</body>
</html>
<?php } ?>

Thank you very much for any help you can offer!

cooldug000
  • 21
  • 2
  • 1
    sounds like the csv should be put in to a db –  Jan 29 '13 at 21:58
  • Can you tell us what is used as the delimiter and what is used as the line break in the CSV file? – Lloyd Banks Jan 29 '13 at 22:32
  • Sorry for my ignorance, but what would be involved in converting the CSV into a database? The CSV came from QuickBooks originally, I'm guessing it's already in a database within QuickBooks. It's delimited with commas, but I don't know what is used as the line break. Are there CSVs that aren't comma separated? – cooldug000 Jan 30 '13 at 00:28

2 Answers2

0

I think that you must use a template for the row and create dynamically the necessary rows with Javascript.

You can add an event to the first field. When it has changed you can send a petition to the server (using Ajax), and populate the second field with the returned data.

You can see other questions like this or this where part of this method is explained.

Community
  • 1
  • 1
0

Apart from the obvious suggestion that Put the CSV in a DB and fetch it from there, you can read the csv file and store it in a javascript object. Like:

var myObject = {
   item_number_1: description_1,
   item_number_2: description_2,
   ....
   ....
   ....
   item_number_n: description_n
}

Then, you can check if the item number entered by the user is in this object via event handlers and if so, display the description. Hope that helps.

Way to create the object from the csv

<script type="text/javascript">
var myObject = {};
<?php
$fp=fopen('PATH_TO_YOUR_CSV_FILE','r');
$str = '';

while(($data=fgetcsv($fp,'',','))!==false) {
   $item = htmlentities(trim($data[0]),ENT_COMPAT,'UTF-8',false);
   $desc = htmlentities(trim($data[1]),ENT_COMPAT,'UTF-8',false);

   $str .= 'myObject["'.$item.'"]="'.$desc.'",';
}
fclose($fp);
echo trim($str,',');
?>
</script>
web-nomad
  • 6,003
  • 3
  • 34
  • 49