Mário Souto Mário Souto - 6 months ago 10
SQL Question

Form dosn't save data into mySQL tables

I'm very new to mySQL, javascript and php. I have a form in my website to register new users into it. Its connected to a users table in my mySQL where it saves the users data.

The thing is I cant 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 wont 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";
}

});

Answer

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();