how to get form data using jquery
how to get form data using jquery
1 Answer
i will use the bootstrap starter template,
before </body> close tag, I will write jquery code to get email and password when a user click on submit a form
Code Explain
- this will stop the form from submission
<form onsubmit="return false">
- set
id="sendMeNow"
to submit form
<button type="submit" class="btn btn-primary" id="sendMeNow" >Submit</button>
- before </body> close tag, I will write jquery
<script>
$("#sendMeNow").click(function(e) {
var Email = $("#email").val();
var Password = $("#password").val();
alert("Your Email is: "+Email+ "\n"+" Your Password is:"+Password);
});
</script>
Finally this is full code
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Send form using Ajax</title>
</head>
<body>
<h1>Hello, Ajax!</h1>
<form onsubmit="return false">
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password">
</div>
<button type="submit" class="btn btn-primary" id="sendMeNow" >Submit</button>
</form>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script>
$("#sendMeNow").click(function(e) {
var Email = $("#email").val();
var Password = $("#password").val();
alert("Your Email is: "+Email+ "\n"+" Your Password is:"+Password);
});
</script>
</body>
</html>
answer Link