Fetching data from a json file using variables javascript [duplicate] - javascript

This question already has answers here:
How do I access a JSON object using a javascript variable
(4 answers)
Closed 8 years ago.
My question is pretty simple. I have a json file stored and I fetch it using a simple xml request and store it for later use in the localstorage. Now What I need to do is that when the user selects something from the select tag, I fetch the value of the tag and use it get the data from json.
For ex:
database.Class.Teachers."The value from the select tag".age
Is this possible with javascript or similar workaround?

Ok i got the answer. Thanks.
link for the working code i found.
How do I access a JSON object using a javascript variable

Related

How to encode a string for displaying in HTML in JavaScript? [duplicate]

This question already has answers here:
Javascript decoding html entities [duplicate]
(4 answers)
Closed 3 years ago.
I have created a webpage to create a post. For taking description, I have used CKEDITOR. When I save data in mongo database it is saved as:
'<p>How are you?</p>\r\n'
But when I fetch this and display it in my ejs file, then I got an error that is:
Uncaught SyntaxError: Invalid or unexpected token
After fetching the content looks like this:
"<p>How are you?</p>"
Can you help me out in this? Thanks in advance.
And also ask me if you need further explanation.
You can decode HTML content by using decodeURIComponent. for more details (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent)
const uri = "<p>How are you?</p&gt";
const decodeUri = decodeURIComponent(uri);
console.log(decodeUri)

Simplest way to store this to do list data even after browser closed? [duplicate]

This question already has answers here:
How to store objects in HTML5 localStorage/sessionStorage
(24 answers)
Storing JSON data in browser memory
(3 answers)
Closed 4 years ago.
I want to make a web-based TO-DO list exactly like the one on the W3schools link below, however when I close the browser all the inputs and edit disappear and it reverts back to the original. Is it possible to easily store the values (if not I will quickly delete this Q)? What code can I add to make it save the data (perhaps localstorage?) each time or would I have to set up a php/mySql? No authentication required, whatever is the most straightforward way to do it.
The exact code is below. I prefer to host locally for simplicty but if necessary web based hosting server is also fine with me.
To Do List Example from W3 Schools
You can use Javascript LocalStorage to save data on browser close, only if it is being loaded on a domain that is accessed via HTTP.
Though, you can use a server-side database, such as mySQL to store data.

How to pass variable value from php to external JavaScript file? [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 5 years ago.
I have a value sitting in my database which i want to use in an external JavaScript file. The JavaScript file is being referenced in the the html code using <script src="/thanks/thanks.js"></script>
Can anyone please help to figure out how i can either pass the value from php tp JavaScript or how i can access the database from the JavaScript?
The simple way is to use the <script> tag and echo the value you want to.
<?php
echo "<script>var parse = $value;</script>";
?>
Now you can use your value in javascript (parse variable). But if you want to have it detailed...
here is the duplicate you didnt search for: How to pass variables and data from PHP to JavaScript?

get data from mysql using php and javascript [duplicate]

This question already has answers here:
Using Jquery Ajax to retrieve data from Mysql
(5 answers)
Updating a MySql database using PHP via an onClick javascript function
(2 answers)
How to update a web page without reloading the web page using AJAX?
(4 answers)
Closed 5 years ago.
I am new to javascript. I have one label, What I am doing is to get id using javascript which I am saving in a variable.
What I want is to send that variable to php page, where it will compare that id with mysql database id and returns it's data(some text) to javascript, it will then set it to label.
I would have used text file which is easiest way but some says this isn't a secure way to do this.
So how can I achieve this ?
Any help is appreciated, Thanks in advance.

Pass an array from .jsp file to .js file [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to transfer java array to javaScript array using jsp?
After googling I am still not able to pass array from .jsp file to js file.
Can somebody help me out?
In my .jsp file i have an array and I wan to call a function in .js file which accepts this array. How to call this function?
By passing an array from .jsp to js file I guess you mean your Javascript script needs to "call" a script return by a JSP file. And that script contains a function that returns an array?
If so have you looked at returning JSON from your JSP?
For simple arrays, you can convert the array to a comma-delimited string using an implode function, pass the string to javascript, and then use split(str) in Javascript to get an array back. More complicated arrays may cause issues with this.

Categories

Resources