0
How to get information from forms in JS
It's for a project
3 Respostas
+ 8
This is an example:
HTML
<form id="myForm">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<button type="button" onclick="submitForm()">Submit</button>
</form>
JS
function submitForm() {
const form = document.getElementById("myForm");
const name = form.elements["name"].value;
const email = form.elements["email"].value;
console.log("Name: " + name);
console.log("Email: " + email);
}
Look at this example. You are taking (by ID) the form from the html document. You use form.elements[elem] to get [elem] from the form. The .value attribute is for taking the information from [elem]
+ 9
When the form is submitted, the contents (fields) of the form are forwarded to another page, where a server side script or program is supposed to process the data.
Example:
https://code.sololearn.com/WcYSaxO4YG25/?ref=app
https://www.w3schools.com/html/html_forms.asp
https://www.w3schools.com/php/php_forms.asp
+ 3
More external reference for those interested
https://developer.mozilla.org/en-US/docs/Learn/Forms/Sending_and_retrieving_form_data