Reputation: 198
Each time I input the data in the fields and click submit, the page refreshes, clears the forms, and not entering on the external Javascript file. But when I change the button type to "button" instead of "Submit", everything goes well except the "required" pop doesn't show anymore. What is the best practice in calling the external script when submit type button is clicked?
HTML file
<!DOCTYPE html>
<html>
<head>
<title>Registration Form 2</title>
<link href="bStrap/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
<link href="style 2.css" type="text/css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div class="row">
<div class="col-md-2">
</div>
<!--MAIN PANEL-->
<div class="col-md-8">
<div class="panel-group">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="pheading">Registration</h1>
</div>
<div class="panel-body">
<!--FORM START-->
<form class="register">
<!--NAME-->
<div class="row">
<div class="col-md-1">
</div>
<div class="col-md-6">
<fieldset class="well">
<legend align="left">Name</legend>
<div class="row field">
<div class="namefields">
<input type="text" class="form-control" id="fname" placeholder="Enter firstname" required>
<input type="text" class="form-control" id="lname" placeholder="Enter lastname" required>
</div>
</div>
</fieldset>
</div>
<!--BDAY-->
<div class="col-md-4">
<fieldset class="well">
<legend align="left">Birthday</legend>
<input type="date" class="form-control" id="bday" required>
</fieldset>
</div>
<div class="col-md-1">
</div>
</div>
<div class="row">
<div class="col-md-1">
</div>
<!--EMAIL-->
<div class="col-md-4">
<fieldset class="well">
<legend align="left">Email</legend>
<div class="row field">
<div class="col-md-12">
<input type="email" class="form-control"
id="email" placeholder="Enter email" required>
</div>
</div>
</div>
<!--GENDER-->
<div class="col-md-3">
<fieldset class="well">
<legend align="left">Gender</legend>
<div class="row field">
<div class="col-md-6">
<label class="radio-inline"><input type="radio" name="gender" checked value="Male">Male</label>
</div>
<div class="col-md-6">
<label class="radio-inline"><input type="radio" name="gender" value="Female">Female</label>
</div>
</div>
</fieldset>
</div>
<!--COUNTRY-->
<div class="col-md-3">
<fieldset class="well">
<legend align="left">Country</legend>
<div class="row field" align="middle" title="Select Country">
<select id="country">
<option selected hidden>Select Country</option>
<optgroup label = "Asia">
<option value="Philippines">Philippines</option>
<option value="China">China</option>
<option value="Japan">Japan</option>
<optgroup label = "Europe">
<option value="Italy">Italy</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
</select>
</fieldset>
</div>
<!--SUBMIT-->
<div class="row">
<div class="col-md-5">
</div>
<div class="col-md-2">
<div class="submit">
<button type="submit" class="btn btn-primary" id="sbtn" onClick="validate()">Submit</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"></h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-5">
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<div class="col-md-2">
</div>
</div>
</body>
</html>
JAVASCRIPT FILE
`function validate() {
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var email = document.getElementById("email").value;
var gender = $('input[name="gender"]:checked').val();
var bday = document.getElementById("bday").value;
var country = document.getElementById("country").value;
if (fname=="" || lname=="" || email=="" || country == "Select Country"){
console.log("Incomplete data");
$(".modal-title").text("Incomplete Data");
$(".modal-body").text("Please Fill All Required Fields");
$("#myModal").modal();
}
else{
$(".modal-title").text("Registration Succesful");
$(".modal-body").text("Welcome "+ fname +"! Your data has been inserted.");
$("#myModal").modal();
var info = new Object();
info.firstname = fname;
info.lastname = lname;
info.email = email;
info.gender = gender;
info.country = country;
info.bday = bday;
console.log("Output: " , info);
}
}`
Upvotes: 0
Views: 84
Reputation: 11
I can't comment on things yet so let me just post an answer.
I agree with theprogrammer's answer, use the onsubmit
event $(...).submit(...) in jQuery
and then use event.preventDefault()
.
To elaborate (and correct me if I'm wrong), the reason that your form "refreshes" the page upon clicking the button is that since your button is type='submit'
when you click it, it submits the form.
The form doesn't actually refresh (which is why I used quotes earlier). It tries to run a certain HTTP method to a certain route (as per HTML forms do on submit). By default, it tries to run a GET method so which is taking you to that route.
So the event.preventDefault()
is actually preventing that behavior from happening, hence why theprogrammer's answer works.
Look up HTTP methods and HTTP verbs to really understand how forms work. I'm unable to provide you links atm.
Hope that helped/made sense.
Upvotes: 1
Reputation: 2734
There are 2 ways to go about it. The one I personally like is to change the button to a type submit and bind the onSubmit event from the form to the validate() js function.
Make sure that to cancel the event if an error occurs, so the form is not submitted using event.preventDefault()
Upvotes: 1