I have an html form as follows:
<form role="form" name="login" id="login" >
<div id="alert-success" class="alert alert-success" style="display: none" ></div>
<div id="alert-danger" class="alert alert-danger" style="display: none" ></div>
<div class="form-group">
<label for="exampleInputEmail1">Name</label>
<input type="name" class="form-control" id="username" placeholder="Enter name" style="width: 30%;">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="usermail" placeholder="Enter email" style="width: 30%;">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Username</label>
<input type="password" class="form-control" id="loginname" placeholder="username" style="width: 30%;">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="loginpassword" placeholder="Password" style="width: 30%;">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
</label>
</div>
<button type="submit" id="submit" data-dismiss="alert" name="submit" class="btn btn-default">Join Us</button>
</form>
A jquery in the same file for validation and to submit form:
$(document).ready(function () {
$("#submit").click(function () {
var username = $("#username").val();
var usermail = $("#usermail").val();
var loginname = $("#loginname").val();
var loginpassword = $("#loginpassword").val();
var emailregex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (username == "" || username.length > 20 || !isNaN(username)) {
$('#alert-danger').show();
$("#alert-danger").html("Please Enter Proper Name");
$("#username").focus();
return false;
} else if (usermail == "" || !emailregex.test(usermail)) {
$('#alert-danger').show();
$("#alert-danger").html("Please Enter Your Email Id");
$("#usermail").focus();
return false;
} else if (loginname == "" || loginname.length > 10) {
$('#alert-danger').show();
$("#alert-danger").html("Please Enter Username");
$("#loginname").focus();
return false;
} else if (loginpassword == "" || loginpassword.length > 10) {
$('#alert-danger').show();
$("#alert-danger").html("Please Enter Proper Password");
$("#loginpassword").focus();
return false;
} else {
var datastring = 'name=' + username + '&email=' + usermail + '&loginusername=' + loginname + '&loginuserpassword=' + loginpassword;
$.ajax({
type: "POST",
url: "userdata.php",
data: datastring,
cache: false,
sucess: function (dataitem) {
if (dataitem == 1) {
$("#alert-danger").html("Something Went Wrong");
} else {
$("#alert-danger").html("Something Went Wrong");
}
}
})
}
})
})
Now I am encountering problem while submitting the form,it does work,as data are saved to database table,but I cannot see my form after it have submitted,I have used location.reload();
function but still its not working ,pls help
Do like this :
$("#submit").click(function() {
// your code
return false;
});
or
in else part
else
{
var datastring = 'name=' + username + '&email=' + usermail + '&loginusername=' + loginname + '&loginuserpassword=' + loginpassword;
$.ajax({
// code ....
});
return false;
}
When you correctly fill the form , it's been submited by ajax and then by on submit of click. Return false stop the next submit and only your ajax post will go.
Add e in the function() and then e.preventDefault() as the first line in the click function:
$("#submit").click(function(e) {
e.preventDefault();
//code here
});
<button type="submit" id="submit" data-dismiss="alert" name="submit" class="btn btn-default">Join Us</button>
应该:
<input type="submit" id="submit" data-dismiss="alert" name="submit" class="btn btn-default" value="Join Us">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.