0

I'm very inexperienced so I'm sorry if this is a stupid question.

I was wondering if it's at all possible to code something on a site that takes an input and "uses" it elsewhere - what I'm going for is an input box that asks for a name, and the name given will appear in place of some sort of "name" variable. For example, if you entered your name as "Bob", something like "Hello (name)" would appear as "Hello Bob".

I haven't really seen anything that gives instruction on how to do this, which makes me wonder if it's even possible.

I've been trying to handle the input box itself, and so far it looks like this:

<body>
  <form>
  <label for="namebox">Name:</label>
  <br>
  <input type="text" id="uname" name="name" value="Namehere">
  <br>
  <button>Submit</button>
  </form>

  <p id="namebox"></p>

  <script>
  document.getElementById("namebox").innerHTML = "uname";
  </script>
</body>

The output of this currently is being sent to a nonexistent page ending in "/?name=(whatever name value is inputted)".

Also, if this does work, would the change to the "name" variable only occur on the page with the input form, or would it carry to all pages on the site?

Thank you for any help.

  • It is of course possible. This site has your screen name on this page, after all, and mine right after this comment. JavaScript and the DOM are the technologies that allow this, in addition to server-side technologies like ASP.NET. However, one has to program the functionality in order for the browser to perform the behavior. – Heretic Monkey Sep 09 '19 at 18:00
  • With javascript alone, you can listen for the keyup or change events from the input box and change whatever you want to on the page using the input's value. If you submit it as a form, you can pull the value out of the URL (if it's sent with get) using window.location or you can handle it with whatever server language you're using, like PHP with `$_GET["uname"]`. This will not carry over to all other pages by itself. You'll have to set that up, again using whatever server language. In PHP, you do that with `session_start()` and `$_SESSION["var"] = $var;` – Liftoff Sep 09 '19 at 18:00
  • Yes, it's possible and quite common. There are many ways to do this. What do you need to happen? – disinfor Sep 09 '19 at 18:01
  • Basically I would want a situation where something like ```

    Your name is [however the "name" variable would be written]

    ``` would display as "Your name is [the name given in the input box].
    – froghaver Sep 09 '19 at 18:07
  • Here's a fiddle with what you're looking for: https://jsfiddle.net/rfehx8gd/ The question was closed as I was posting an answer. – disinfor Sep 09 '19 at 18:34
  • So I guess I still have problems - this code only works to change the name value of a single instance, and won't work for any other usage of ``````. According to the jsfiddle this is because ```[namebox]``` and ```[username]``` "must be unique". How would I go about using the inputted name multiple times on the same and other pages? – froghaver Sep 09 '19 at 19:45

1 Answers1

-1
<script> 
document.getElementById("namebox").innerHTML = "Hello" + document.getElementById("uname").value

</script>
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
vijai
  • 1
  • 2
  • That works! Is there a way to make it so it doesn't redirect to another page ending in the given input? – froghaver Sep 09 '19 at 18:14
  • If you remove a form tag. It does not redirect to any other page – vijai Sep 09 '19 at 18:17
  • Upon removing the form tag it no longer redirects to any other page, but it also doesn't use the input anymore. The "Hello" won't have the input next to it. – froghaver Sep 09 '19 at 18:19
  • Otherwise just use return false while submitting a form Example: $('form').submit(function(){ return false;}); – vijai Sep 09 '19 at 18:26
  • This code ```html


    ``` results in the "Hello" message disappearing as well as still redirecting to a different page upon submission of the input
    – froghaver Sep 09 '19 at 18:31