I’m trying to insert information from the form to the Database.
This is the form in html – tried both post and get.
<form id="register" action="script/register.js" method="post">
<label for="username">Username:</label>
<input type="text" name="username" id="username" placeholder="Username" />
<br>
<label for="passd">Password:</label>
<input type="password" name="passd" id="passd" placeholder="Password" />
<br>
<label for="conpassd">Password:</label>
<input type ="password" name="conpassd" id="conpassd" placeholder="re-type Password"/>
<br>
<label for="email">E-Mail:</label>
<input type="text" name="email" id="email" placeholder="Email address"/>
<br>
<br>
<button type="submit" name="submitMe" id="submitMe">Submit</button>
<button type="reset">Clear</button>
</form>
This is the script
$(document).ready(function() {
var usernameCorrect = false;
var passwordCorrect = false;
function checkPassword() {
var p1 = $("#passd").val();
var p2 = $("#conpassd").val();
if(p1 == p2) {
passwordCorrect = true;
} else {
passwordCorrect = false;
alert("Passwords does not match");
}
}
function checkUsername() {
var user = $("#username").val();
var dataPass = {
"username": user
}
$.ajax({
url: "server/userExists.cgi",
type: "GET",
dataType: "text",
data: dataPass,
success: function(data) {
var user = $.trim(data);
if(user == "No") {
usernameCorrect = true;
} else if(user == "Yes") {
usernameCorrect = false;
alert("Username already in use");
}
},
error: function() {
alert('Script Execution Error');
},
});
}
function submitForm() {
if(usernameCorrect == true && passwordCorrect == true) {
e.preventDefault();
var dataPass = {
'username': (username),
'passd': ($("#passd").val()),
'email': ($("#conpassd").val())
};
$.ajax({
url: "server/init.cgi",
type: "GET",
data: dataPass,
success: function() {
alert("Register successful");
//nextPage(username);
},
error: function() {
alert("Error!");
},
});
}
}
$("#submitMe").click(function(e) {
submitForm();
});
});
Is there anyway to link the form to javascript? I don’t know any other language such as php so…
Thanks
A few errors
checkUsername()andcheckPassword()sousernameCorrectandpasswordCorrectare still false.Here’s what you need to change, Your HTML for the form should be
And your script had to be adjusted slightly because of the asynchronous nature of AJAX calls, you have to use callbacks.