Javascript A thank you page with user name on it - javascript

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/

Related

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

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

How can I transfer the information I get from the form to another page?

How can I show the information I got from the place where I entered the title and text on the announcement page?
repo:https://github.com/qswezy/spa1
We have added the form and we want to show it in the announcements
Forms usually have target page. So, this is the page that will receive your submitted data, usually in some type of POST array (if you submitted it as a post.) If this assumes JavaScript, you probably need to run an Express server to do this.

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.

Remeber file selection

I have a form which asks to select a file for upload. PArt of the form requires the user to click through to another page. This is fine as I can store field values in local storage. The problem is the file selection - is there a way to remember what the user selected once they navigate back to the form?
If not can anyone suggest how to implement what I am trying to do?
Thanks
So as you are not posting some code examples, I assume that your are not interested in code suggestions.
You could use a session to store some data the user gave you over a form. CakePHP comes with the nice session helper, which makes it easy to deal with.
How to use sessions with CakePHP you can find here:
http://book.cakephp.org/3.0/en/development/sessions.html
So everytime the user fills out the form you could store the relevant data inside the session. Inside your view you just check if there is a value for that data in your session. If yes render the session value as default if not you can render the default value.

Javascript form hidden visible actions

Im trying to make a "coupon" form. So that when my users go to the text field and type in 'hello' persay it then in turn will open the form that corresponds to the code. I am going to make various words/codes to use for the coupon. I'd like to make it so that there is no way they can bypass this, and that they can not see this javascript if they click the view source file. Can someone help me. It seems simple I believe, though I do not know anything about javascript. so //comments would be perferred so that I understand each command and statement happening for further use.Thanks ahead of time.
You can't hide javascript because the javascript has to be accessible for the browser to execute it. And if it is accessible to the browser then it is possible for anyone to access it manually through the URL.
If you don't want users to see the code that handles coupon values, you need to do it in the server-side, not in javascript.
You can use javascript to make an ajax call that sends to the server the coupon value entered. The server side can use this value to choose and return the HTML that corresponds to the coupon value. But the code that checks the coupon value has to be in the server-side if you want it to be hidden.

Categories

Resources