English Getting data from HTML page

10 replies
Goto Page
To the start Previous 1 Next To the start
19.07.17 08:13:21 pm
Up
Masea
Super User
Offline Off
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.
Code:
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.

Code:
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.
Create your UI easy and fast: UI Framework | Go deeper into the darkness and test your bravery: file cs2d Outlast 2 Modification (27)
19.07.17 08:41:48 pm
Up
Zeik
User
Offline Off
@user Masea: You didn't change the get argument:

Code:
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.
19.07.17 09:19:27 pm
Up
Masea
Super User
Offline Off
@user Zeik: You have to understand what I am trying to do first.

Code:
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.
Create your UI easy and fast: UI Framework | Go deeper into the darkness and test your bravery: file cs2d Outlast 2 Modification (27)
19.07.17 10:03:17 pm
Up
Hador
User
Offline Off
I would solve the problem by using jQuery (you could use AJAX and so, but jQuery just simplifies it soo much).

Code:
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:

Code:
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();
edited 1×, last 19.07.17 10:05:26 pm
19.07.17 10:05:07 pm
Up
ohaz
User
Offline Off
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.
https://ohaz.engineer - Software Engineering
19.07.17 10:34:46 pm
Up
Masea
Super User
Offline Off
@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?

Code:
<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?
edited 1×, last 19.07.17 10:37:09 pm
Create your UI easy and fast: UI Framework | Go deeper into the darkness and test your bravery: file cs2d Outlast 2 Modification (27)
19.07.17 10:36:24 pm
Up
Yates
Reviewer
Offline Off
@user Masea: You can set a cookie or a session.

But the idea doesn't seem as if it should work with just a frontend.
19.07.17 10:40:35 pm
Up
Zeik
User
Offline Off
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).
19.07.17 10:41:23 pm
Up
Masea
Super User
Offline Off
user Yates has written:
@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.
Create your UI easy and fast: UI Framework | Go deeper into the darkness and test your bravery: file cs2d Outlast 2 Modification (27)
20.07.17 12:05:16 am
Up
Zeik
User
Offline Off
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.
20.07.17 11:41:04 am
Up
Gaios
Security Supporter
Offline Off
Better to do it in PHP to don't get hijacked. Of course you can use JS to execute PHP script every 1 second.
To the start Previous 1 Next To the start