I'm using mySQL, javascript and php. I have a form in my website to register new users into it. It's connected to a users table in my mySQL where it saves the users data.
The thing is I can't apply the javascript constraints to the field forms unless I have event.preventDefault();
, it will just connect to my php page no matter what and save the data into my table.
But if I use the event.preventDefault();
it will apply the constraints but when I fill it correctly and click register it won't save anything, just creates an empty user in mysql table.
How can I solve this?
var constraints = {
email: {
email: true,
presence: true,
},
Telefone: {
presence:true,
format: {
pattern: "[+0-9]+",
message: "apenas pode conter números [0-9]"
}
},
pwd: {
presence: true,
length: {
minimum: 5
}
},
cpwd: {
presence: true,
equality: {
attribute: "pwd",
message: "as passwords não coincidem"
}
}
}
$("#validationForm").submit(function(event){
event.preventDefault();
$("#error").html("");
console.log(event);
var errors = validate($("#validationForm"), constraints);
if (errors) {
$("#submited").html("");
for (var key in errors) {
$("#error").append(errors[key] + "<br />");
$("#" + key).css("border","1px red solid");
}
}else {
// $("#submited").html("Form Submited");
// redirect to php
window.location.href="registar.php";
}
});
That's because you are manually redirecting to your PHP page.
Instead of this line:
window.location.href="registar.php";
You should rather
document.getElementById("form_to_submit").submit();