How do I get a value from html and post it? - javascript

im a begginer web developer and i want to make something like that:
you login with username end password, then it redirects you to the home page, and says something like this:
Hello (username) welcome!
but i dont know how to read the value from the input please help
i searched it up but it only showed a search input option

This is too generalized for such a question. Many questions: -Are you using any backend? If so which one (what language? And what kind of database (ie SQL etc.))?
To store data you need a backend or at least use frontend js's localStore (would work if every page is on the same domain and server).
Yet another front-end way would be to pass the information as url-parameters and get them.
But if you really want to build a robust login system of any kind, the best way is to use backend (PHP/node JS/Java/python etc.) and store the data in a database (SQL etc.). And make sure it is secure, but security is too broad of a topic for this question.

Your asked very generalized question, for my assumption there you want to do either one of these
when you login, you want to keep username from input and and redirect the user to home page and show the username on the label like hello abcd
You have an input field with username on home page and want to show the text of the input field into label
so, I will give answer considering the above scenarios, the answer will be considering how I will do things(as I don't know what you are using in backend the answer may vary)
for point 1 I will store the username in a localstorage, sessionstorage or cookie.
let's say I used sessionstorage, the below code will be performed on my login button click
sessionStorage.setItem('quantity',document.getElementById('username').value);
and on my pageLoad of home page I will set the value in my label like below
document.getElementById('lblusername').text = sessionStorage.getItem('quantity');
option 2,
on my home Page I will simply get the value from input field and show it on label
document.getElementById('lblusername').text = document.getElementById('username').value;
let me know if it helped you as I don't know your exact scenario

Related

c# QueryString how to send data from one webpage to another?

I wanted to ask a little complicated question for me. I want to make a simple program like I have website www.example1.com and there would be a simple panel to enter your name and your country. After that you have to click "send" and it will give you a request number like 12345. The data entered on that panel will be sent to the another site at wwww.example2.com . And I will check the data given from example1 to example2 (like: name: John , country : Jamaica , request number :12345) . I found on internet something like querystring at c# but im not sure what to do. I demand your help , best regards.
Yes this is possible you could navigate the browser from site www.example1.com to www.example2.com/someAction?name=joe for example. However, in order to validate against this you would need some sort of server in which example1 would have to send the data first and store it. This would then allow example2 to call that service checking the data it got via query params was valid. This is a very long winded way to pass data about not sure it is a good solution.

Is getting an echo'd username using Javascript with display none CSS bad for security?

I have a PHP page that can only be accessed by logged in users. Each page is unique to that particular user and is used for collection tracking.
Through PHP I have the username and user id echo'd and then wrapped in a display:none div.
I then have a couple of ajax calls that pass the username and user id when they do specific tasks (update collection, add to, delete, etc etc). This is passed to a PHP file which uses prepared statements.
I do this since I can't seem to find another way to grab the username and user id in Javascript.
Since each page is accessed only by that particular user, I'm thinking it's safe since you can't get access to another users username or ID. However I can't help but feel that this is extremely bad practice. I'm completely open to suggestions on this!
EDIT: I should also point out that I am using WordPress for authentication.
One thing to think about is ensuring you sanitise the values when you read them. For example what would happen if someone manually set the username in that div to DROP ALL TABLES; - a sql injection attack. Also why not have it as a JS variable by echoing it in a script rather than a hidden div? Otherwise it seems fine, indeed most web applications serialise data into html template to be read by scripts.

How to get part of a link and input it into website with javascript

So I'm trying to make a website where you put in a username and it will give you data about that username using an API. Right now I have it set up so you have to type the username in every time you're on the website, but I was wondering if it's possible to put the username in the link and still be able to retrieve it to put it into an API and use the API's response in the website?
For example, would it be possible to have something like http://website.com/?username=XXXXXXXXXand to be able to get the part after ?username= and to save it to a string or something to put into the API using JavaScript or jQuery? Any help would be appreciated :)
why don't you saving username in cookie/storage/session after user input?your ways is complex and unreliable.or may be you could use the rest url format like this: http://{username}.example.com/api or http://www.example.com/{user}/api then your backend always retrieves username from this path and you needn't add parameters manually.

How can I make a search box to search in a local database?

I'm playing around with Apache Cordova and I want to implement a search box that can search in a list or something similar (local database?). I want it to be local so that the app does not require an internet connection to search.
The way I want it to be set up is that it need to be able to search for both the name of the item and a number.
Example 1:
A user searches for: "Honda". He will then be sent to a page where information about "Honda" will be diplayed.
Example 2:
A user searches for the id: "1337". He will then be sent to the page containing information about "Honda".
So "Honda = 1337" and "1337 = Honda" if you guys understand?
The question:
I'm wondering what solution would suite my needs for this project?
or there any frameworks out there for this?
Thanks in advance!
It depends on what you want to have the search box and submit to be like.
One way is to use this really great tool http://ivaynberg.github.io/select2/ it can load data from database (using either ajax or load all of your data and only filter them) and shows them in selectbox. You can have submit button or something like that next to your selectbox.
Other way might be to use http://www.datatables.net/ it has search box, which can load data to table using ajax where you can have additional information and submit button for sending user to specific page.
There is plenty of other tools for this kind of problem, I used these two and I can say

Javascript A thank you page with user name on it

I have a form where a user input name and other contact information. I validate the input and then redirect to a thank you page. I wanted to personalize the page by having the user name appear as part of the thank you note. I tried document.getElementById(Name1).value to retrieve the value of the Name but it it is giving Undefined.
Is there a way for me to set the value of the Name1 field to another variable that I call again to include it in my text message?
You are getting undefined because in the thank-you page, you don't have the form anymore.. the best way to do it is on the server side.
If you insist on doing it completely on the client side, then you might need some hack. One idea is to use window.location.hash as #MateiMihai mentioned. Another idea is to use local storage which saves values on the client side. This can be helpful: http://www.jstorage.info/

Categories

Resources