-4
<label for="fullname" name="identity">Name, Surname</label><br/>
<input type="text" name="info" value="" />

Once user types inside it something, I want save that into variable. Later be able to use that elsewhere.

<input type="submit" value="send" onclick="myFunction()"/>

What function to create and what to do to to use this value?

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
Tomas
  • 19
  • use `document.getElementById` and get value. This will help https://www.w3schools.com/jsref/prop_text_value.asp – Sanchit Patiyal Nov 14 '17 at 19:20
  • Study some tutorials on how to use javascript and forms. Stackoverflow is not a *"how to"* tutorial service – charlietfl Nov 14 '17 at 19:20
  • Possible duplicate of [How do I get the value of text input field using JavaScript?](https://stackoverflow.com/questions/11563638/how-do-i-get-the-value-of-text-input-field-using-javascript) – klenium Nov 14 '17 at 19:22

1 Answers1

0

If you want to keep the data on the same page, then you will be able to use vanilla JavaScript by declaring a variable at the document level scope. Here is a quick example of that:

window.onload = function() {
  document.getElementById("my_form").onsubmit = my_form_submit;
}

var fullname;
function my_form_submit(e) {
  // Stop the form from submitting
  e.preventDefault();

  // Store the value
  fullname = document.getElementById("fullname").value;
}
<form id="my_form">
  <label for="fullname" name="identity">Name, Surname</label><br/>
  <input type="text" name="info" value="" id="fullname" />
  <input type="submit" value="send" />
</form>

If you want the data to persist across pages, then you'll need to use a server-side language to create cookies. In PHP you'd establish a session and then set the session variable. Here is a quick example of that:

<?php
// Set session variables
session_start();

// Assign the session variable from the form's post
$_SESSION["fullname"] = "info";
?>
David
  • 5,877
  • 3
  • 23
  • 40