0

How to get information from forms in JS

It's for a project

27th Apr 2023, 7:34 AM
Welcome To Dodgeball
Welcome To Dodgeball - avatar
3 odpowiedzi
+ 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]
27th Apr 2023, 8:38 AM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 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
27th Apr 2023, 8:04 AM
Tibor Santa
Tibor Santa - avatar
27th Apr 2023, 10:20 AM
Bob_Li
Bob_Li - avatar