This is my first time coding, and I'm trying to create a feature on my website where after I select an item from the dropdown menu, values corresponding to that item will show up in the input boxes I created. An example is if I select "apples" from my dropdown menu, the values "1.2" and "4.00" will show up in the "Pounds" and "Cost" input boxes respectively.
I already have code written using HTML and JS for the dropdown menu and input boxes, and I have stored the data corresponding to the items in a csv file. Right now, values show up in the input boxes only because I wrote code for that in JS. Is there any way for the HTML file to read my data from a csv file and display that data in my input boxes? Below is what I have so far.
<div id="cs-element">
<label id="cs-element-label">Elements</label>
</div>
<select onChange="dropdownTip(this.value)" name="search_type" style="margin-right:10px; margin-top:2px;">
<option selected="selected">Choose Element</option>
<option>Methane</option>
<option>Ethane</option>
<option>Propane</option>
<option>n-Butane</option>
</select>
<div id="result"></div>
<script type="text/javascript">
function dropdownTip(value){
console.log(value);
document.getElementById("myText").value="190.6";
document.getElementById("myText1").value="45.99";
document.getElementById("myText2").value="0.012";
}
</script>
<div id="cs-tc" class="col-sm-4 text-center">
<label id="cs-tc-label" class="control-label">Critical Temperature (K)</label>
<input type="text" id="myText" value=" " class="form-control" name="cs_tc">
</div>
<div id="cs-pc" class="col-sm-4 text-center">
<label id="cs-pc-label" class="control-label">Critical Pressure (atm)</label>
<input type="text" id="myText1" value=" " class="form-control" name="cs_pc">
</div>
<div id="cs-acc" class="col-sm-4 text-center">
<label id="cs-acc-label" class="control-label">Accentric Factor</label>
<input type="text" id="myText2" value=" " class="form-control" name="cs_acc">
</div>
<script>
$(document).ready(function() {
$.ajax({
type: "GET",
url: "B1_1.csv", //read csv file
dataType: "text",
success: function(data) {processData(data);}
});
});
function processData(allText) {
var allTextLines = allText.split("/\r\n|\n/");
var headers = allTextLines[0].split(',');
var lines = [];
for (var i=1; i<allTextLines.length; i++) {
var data = allTextLines[i].split(',');
if (data.length == headers.length) {
var tarr = [];
for (var j=0; j<headers.length; j++) {
tarr.push(headers[j]+":"+data[j]);
}
lines.push(tarr);
}
}
}
</script>
If I understand what you're asking, no, is not possible to read file from html. Using a form in html you can upload files, but the event is handled by javascript.
Yes, of course is possible, the code you've posted is doing that, I know that initially can be confusing, but let me explain the javascript part...
<script>
// in html you use script tag to put javascript code inside and using jquery, you // can easily modify the html page.
$(document).ready(function() {
// this is a jquery piece of code that is calling an // ajax request
$.ajax({
type: "GET",
url: "B1_1.csv", //read csv file
dataType: "text",
success: function(data) {processData(data);}
});
});
function processData(allText) {
// a method is a piece of code that you write once because you know that you will // reuse it and it makes the code more readable
var allTextLines = allText.split("/\r\n|\n/");
// split is a method that takes as input a string and (in this case `allText`) //literally split the calling string in n strings storing them in an array. The //strings are splitted by the string you passed as parameter. In this case this //piece of code is simply dividing the entire text in n lines
var headers = allTextLines[0].split(',');
// Now is splitting the line number 1 (look for 0-indexed array on google) in many // strings
var lines = [];
for (var i=1; i<allTextLines.length; i++) {
// A for loop is a loop that repeat itself n-times until a condition is no more
// satisfied, in this case i has to be smaller than the length of that array.
// `var i=1` represents the starting point of the index.
// `i<allTextLines.length` is the condition and `allTextLines.length` returns the
// length of the array.
// In the end `i++` just increment i every time the loop reach the end and restart
var data = allTextLines[i].split(',');
if (data.length == headers.length) {
// an if statement is a piece of code that is executed if and only if a condition
// is satisfied.
var tarr = [];
for (var j=0; j<headers.length; j++) {
tarr.push(headers[j]+":"+data[j]);
}
lines.push(tarr); // The push method add to an array a new element
}
}
}
</script>
I tried to explain as better as I can (I'm a bad teacher, so this is training for me ahah), I hope I've helped you. If you have any doubt just ask and if you want to get more into code just lookfor tutorials on the web, because there are a lot. Maybe you're interested, I've learned javascript from here years ago.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.