External JSON file string to HTML output [duplicate] - javascript

This question already has answers here:
read an external local JSON file into Javascript
(4 answers)
Closed 3 years ago.
I have an external JSON file on my server that I want to display the objects from into an HTML document without using JQuery. I specifically just want to call the username object and display it on an "All Users" page for a simple homework project. I've had a difficult time finding ways to do this without JQuery. How could I go about doing this?
JSON Example:
{"name":"asfd","username":"awsf","email":"kean","age":"21","gender":"Male","submit":"Submit"},
{"name":"asdf","username":"asfd","email":"asdf#asdf","age":"21","gender":"Male","submit":"Submit"},
{"name":"null","username":"null","email":"null#gmail.com","age":"21","gender":"Male","submit":"Submit"},
{"name":"null","username":"null","email":"null#gmail.com","age":"21","gender":"Male","submit":"Submit"},

If you have an array of json objects you can loop over it and call JSON.parse() on each. That will give you simple user object that you can access and place in your page his username.
for(var i=0; i<users.length; i++) {
var user = JSON.parse(users[i]);
// you can now access username for each user with user[i].username
// place a username into new element in your page
}
Useful reference for making server call without jQuery : How to make an AJAX call without jQuery?

Related

Retrieving Javascript variable into Php code from empty div tag [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 2 years ago.
1. <script>
2. document.getElementById('description').innerHTML = description;
3. </script>
4. <div id="description"></div>
How to get the value of line 4 into PHP code?. Also when I use the traditional way
<?php $weather = <div id = "description"></div>?>
The weather variable is not displayed wherever I want it to be displayed.
Any help is greatly appreciated. Thank You.
The PHP is processed on the server side, while the Javascript value is processed in the user's browser. So when the page is sent, there is no value there yet.
You can do this:
<?php
$weather = '<div id="description">Details go here</div>'; // define variable
echo $weather; // include / print variable
?>
But if the value does not exist yet that you want to include, then you need to use AJAX to send it to PHP if that's indeed what you need. Although if it doesn't need to go back to the server, it can likely be done just with JavaScript on the client side.
I suggest finding some good books on learning PHP / learning JavasScript / learning HTML.

how to get url parameters to appear on an html page WITHOUT php [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 2 years ago.
I am developing a site in HTML (not PHP) and am trying to get url parameters to display on an appointment confirmation page. The appointment confirmation info is successfully passed to the URL parameters. But I can't figure out how to get them to appear on the page.
For example:
?name=John&date=Nov-26&Time=4pm
to appear on the html page as:
Name: John
Date: Nov 26
Time: 4pm
I have watched tutorials on youtube, followed instructions and copied code from examples on stackoverflow, but the results are a complete failure. Nothing appears on the pages at all.
If you reply, could you please provide the full code as I do do not know javascript and its exceedingly difficult for me to try to figure out where added options get inserted into any base coding you provide. For instance, if there is code with "var" in the option, does that replace the "var" that is in the base code offered, or does it get added to it? If it gets added to it, where does it go? Does it go after the semi-colon, or do I need to start a new curly bracket after the closed curly bracket?
Thanks in advance.
Try this - I am using URLSearchParams to extract the values from the URL and template literals to create the string
Change "?name=John&date=Nov-26&Time=4pm" to location.search on your site
<footer id="footer"></footer>
<script>
const srch = "?name=John&date=Nov-26&Time=4pm"; // location.search;
const parms = new URLSearchParams(srch);
document.getElementById("footer").innerHTML += `Name: <span id="name">${parms.get("name")}</span>
Date: <span id="date">${parms.get("date").replace(/-/g," ")}</span>
Time: <span id="time">${parms.get("Time")}</span>`;
</script>

Displaying value transferred from URL via javascript [duplicate]

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 3 years ago.
I'm having an issue with my codes concerning the transfer of information from one page to another using the url as shown below:
window.location.href ="form.html?uname="+uname;
The value is displaying in the url box but when I try to display it on the form.html page using the following code:
window.onload = function ()
{
var name = document.getElementById("uname");
alert(name);
}
The alert keep displaying null.
What is the issue because after an hour of troubleshooting, I can't seem to figure it out.
Is the null being displayed in the alert box means that the value is not being retrieve from the url?
Thanks in advance.
document.getElementById('uname')
looks for an HTML element with the corresponding id. What you probably want to do is:
alert(uname)

Retrieving parameter from query string based on another string in Javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
My app in Google Apps Script gets data from a form that I want to process.
The element form is received and contains the different parameters and values.
If I log the element form it looks like {uid=11, tradingname=xxx, email=yyy}
I can pick up the separate values by running form.uid or form.tradingname for example. But what I want is to pick up the parameters from the element form by referring to a String "uid" (because I get this value form the headers row of the spreadsheet).
Is there a simple way I can do this in one line, for example something like:
form.element["uid"]
So far I've only found difficult methods like https://gomakethings.com/how-to-get-the-value-of-a-querystring-with-native-javascript/ and I would think that I should be able to do this in an easier way.
Thanks!
You can use $("#yourform").serializeArray() and then iterate to match "name" values you want

Pass json message notification in javascript chrome extenstion [duplicate]

This question already has an answer here:
send JSON data in send notification using parse.com (JavaScript)
(1 answer)
Closed 8 years ago.
Folloing working for me in javascript for notification alert("message onclick"); this way i want to pass json string message in push notification , i dont know how to pass this please help me. i want something like this,
alert("\{"action":"com.example.dictionarylib.notification.UPDATE_STATUS", "alert":"hello""\}");
but i dont know actual sysntax of passing json, please give me right way to pass this.
As I understand you need a function which will convert your JSON object to String.
There is function called stringify you can use for that.
Here is the documentation on stringify function.
And here is the usage example:
var json = {
"action": "com.example.dictionarylib.notification.UPDATE_STATUS",
"alert": "hello"
};
alert(JSON.stringify(json));
JSFiddle Demo.

Categories

Resources