The below code is meant to allow the user to fill in the fields and then save their details to the database table, although before adding to the database the form must be validated so blank or incorrect information can not be submitted. there is php code to match, and the validation does work if either the ‘action’ or ‘onsubmit’ is deleted but all three can’t work at once. how to fix this?
i am new to php and completing a simple website for a project, how do i do all as one script?
i have changed the form so users can insert their own username and password, i have taken away the action=“sendmail.php” the onsubmit=“saveUser()” is as follows:
function saveUser()
{
var xmlhttp;
xmlhttp= new XMLHttpRequest();
xmlhttp.open("GET", "insert.php?&firstName="+document.getElementById("firstName").value
+"&secondName="+document.getElementById("secondName").value
+"&username="+document.getElementById("username").value
+"&password="+document.getElementById("password").value
+"&email="+document.getElementById("email").value, false);
xmlhttp.send(null);
alert("Record insert successfully!");
}
wheni hit the submit button now, the records are added to the database but once the form is submitted and records added then the validation kicks in, how can i make this happen before?
I don’t see any validation, but if you want to handle the form submission on client side you need to prevent the default action:
var form = document.querySelector('form')
form.addEventListener('submit', function validate (event) {
event.preventDefault()
// Do some basic client side validation
// Note: this can't replace backend validation!
// If everything is okay, ssubmit the form
// using AJAX
saveUser()
})