Forum

> > Off Topic > Getting data from HTML page
ForenübersichtOff Topic-ÜbersichtEinloggen, um zu antworten

Englisch Getting data from HTML page

10 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Getting data from HTML page

Masea
Super User Off Offline

Zitieren
Hello. I'm trying to learn some HTML, CSS and as well as JavaScript stuff. Here's a something that I got stuck at:

Ya know, unrealsoftware ensures to get a user name by id or id by user name.
1
http://www.unrealsoftware.de/connect.php?getname=USERID
So, how to take the name here with JavaScript? Or can I get with HTML? I don't know.

1
2
3
4
var url_string = "http://www.unrealsoftware.de/connect.php?getname="+v;
var url = new URL(url_string);
var c = url.searchParams.get("c");
alert("you are logged-in successfully. welcome "+c+"!")
I tried something like above but it says "null".

I'm really new to these languages, so I'm hoping there'll be someone willing to help me. Thanks in advance.

alt Re: Getting data from HTML page

Zeik
User Off Offline

Zitieren
@user Masea: You didn't change the get argument:

1
2
3
4
var url_string = "http://www.unrealsoftware.de/connect.php?getname=USERID";
var url = new URL(url_string);
var c = url.searchParams.get("getname");
console.log(c);

I don't know what the v variable is so I didn't write it.

HTML tells the browser how the webpage is structured at load time (and CSS styles the HTML elements). After that it's pure Javascript.

alt Re: Getting data from HTML page

Masea
Super User Off Offline

Zitieren
@user Zeik: You have to understand what I am trying to do first.

1
http://www.unrealsoftware.de/connect.php?getname=USERID
As you can see, there's "USERID" - that part can be filled by you or someone else. As an example, let's put your USGN ID there, which is 12029: http://www.unrealsoftware.de/connect.php?getname=12029 So click it, you'll see your name.

So what I am trying to do is, get the name of "v". "v" is a USGN ID on my code.

How can I get that into my HTML page? That's the question. Thanks again.

Maybe it's really easy as I couldn't even imagine and as maybe someone is even telling me "omg this guy is idiot". I need to find the way.

alt Re: Getting data from HTML page

Hador
User Off Offline

Zitieren
I would solve the problem by using jQuery (you could use AJAX and so, but jQuery just simplifies it soo much).

1
2
3
var id = "9662";
var url = "http://www.unrealsoftware.de/connect.php?getname=" + id;
var name = $.get(url);


This is the equivalent using pure javascript:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var id = "9662";
var url = "http://www.unrealsoftware.de/connect.php?getname=" + id;
var name = "";

function getName() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     name = this.responseText;
    }
  };
  xhttp.open("GET", url, true);
  xhttp.send();
}
getName();
1× editiert, zuletzt 19.07.17 22:05:26

alt Re: Getting data from HTML page

ohaz
User Off Offline

Zitieren
You need some kind of backend for that. This can't be done frontend only. You see, the frontend is what runs on the client side - that's javascript, css and HTML. The backend is what runs on the server. This part can access database data. The frontend can't.

There are some backend Javascript solutions (Meteor or Express.js), most backends are in other languages though. For example, the UnrealSoftware backend is written in php.

alt Re: Getting data from HTML page

Masea
Super User Off Offline

Zitieren
@user Hador: Thank you, I'll try that!

@user ohaz: Yes, yes, yes. At this point, I need a quite big help. Can you tell me exactly what should I need to learn? How can I do those things?

<script type="text/javascript" src="codes.js"></script>
And something like this can be reachable?

It's been like 4 days before I started to learn this kind of stuff, so I got a lot of questions. One of them is pretty confusing for me. I'll ask that to you when I completed what I'll say: In these 4 days, I've been trying to do stuff for my mod. A player who logged-in through his USGN ID, can type "!code" to the chat in-game and get his code. These codes of players stand for login into my HTML page. There are two inputs; first is USGN ID and second is the code.

For example, I typed "!code" to the chat while in-game and then get my code which is "2732". Then I go my HTML page, I write there my USGN ID and that code, then press the "submit" button which means I'm now logged-in successfully. I save those codes to a .js file and run them with the code(not the code I get by typing in-game, the actual code written in green area) above.

So what I'd like to learn is, okay I made login system but how can I keep people logged-in? I mean, like I logged-in but whenever I refresh the page or roam among the pages, I'll lose my account which was logged-in already. Do I need to learn PHP for that?
1× editiert, zuletzt 19.07.17 22:37:09

alt Re: Getting data from HTML page

Zeik
User Off Offline

Zitieren
Oh.. I thought you were trying to get parameters from the url. If you want to get a result from a url then you need to make an HTTP request. For that you use Ajax as Hador said. jQuery makes it way easier to make an HTTP request (and anything else).

alt Re: Getting data from HTML page

Masea
Super User Off Offline

Zitieren
user Yates hat geschrieben
@user Masea: You can set a cookie or a session.
I heard something like session before when I was watching some lesson videos about PHP. Looks like I need to watch them much more to get what is what, you know.

alt Re: Getting data from HTML page

Zeik
User Off Offline

Zitieren
You should be clearer about what you're trying to do...

If you load a js file in HTML it will be loaded with the HTML (once). It won't be updated until the client refreshes the page. And you should know that JS is client-side so it can be read by anyone who loads your page (and modified). And if you save anything within a javascript it will be saved locally only (in the client's machine), unless you make an HTTP request to a server.

For login you need server-side code so you can keep a session between pages. You can then store data in a database or files or whatever.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antwortenOff Topic-ÜbersichtForenübersicht