Load csv file into javascript file
Is it possible? I want to pull the price of a product from a csv file and link that price to its specific part number.
I am doing this for a website and I am learning script programming at the same time and I am not sure how.
The js file will call the csv file and pull the price and part number from it.
From there, a JS file will create a form for use with Paypal cart.
This is the PayPal Cart Code for the Add to Cart button:
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="XXXXX">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="PartNumber1">
<input type="hidden" name="amount" >
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="button_subtype" value="products">
<input type="hidden" name="cn" value="Add special instructions to the seller">
<input type="hidden" name="no_shipping" value="2">
<input type="hidden" name="weight" value=".5">
<input type="hidden" name="weight_unit" value="lbs">
<input type="hidden" name="add" value="1">
<input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHosted">
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
Using a variable added to this button, the JS file would look something like this:
function CalculateOrder(form)
{
if (form.item_name.value == "PartNumber1"
{
form.amount.value = (The price variable would go here);
}
if (form.item_name.value == "PartNumber2"
{
form.amount.value = (The price variable would go here);
}
}
What I need before this code in the JS file is the code that loads the csv file and outputs the price and part number. The JS file will look for a part number named "PartNumber1" and output the corresponding price from the csv file.
HELP PLEASE !!
Perhaps, but perhaps more problems than it is worth.
First you need to get the CSV data. It's easy as long as it's on the same server as the site. XMLHttpRequest is your friend. You can get the raw CSV from responseText.
Then you need to analyze it.
If you have a very simple CSV file, you can split first on new lines (to get each line) and then on a comma (to get each column).
Without testing, something like this might do the trick:
var csv = myXHR.responseText;
var rows = csv.split("\n");
for (var i = 0; i < rows.length; i++) {
rows[i] = rows[i].split(",");
}
Then you can find the part number you want:
var getPart = function getPart(partNumber) {
for (var i = 0, j < rows.length; i < j; i++) {
if (row[i][partNumberColumnIndex] === partNumber) {
return row[i];
}
}
};
However, CSV is not as simple a data format as it looks at first .
If any of the data has a comma or newline in it, you cannot just separate those characters as you need to watch out for the quotes.
If I had CSV data that I really wanted to process on the client, I would use Text :: CSV and JSON :: Any to convert to a format that JavaScript can parse natively before sending it to the client.
For this particular task, I would probably forget about doing it completely on the client side and just doing everything on the server. I don't see much benefit in pushing this work off to the client. (Although admittedly I don't know all the details).
a source to share
I once wrote a script that downloads CSV and displays dots . This might be a good starting point for you.
a source to share
If you want a JavaScript CSV parser that supports the more complex parts of the CSV RFC, you can take a look at my parser implementation - but note that this is only the first version and still needs a bit of cleanup.
a source to share