Reputation: 97
What's wrong with this code? I almost sure that something's wrong with the form's submitting: not getting any of alerts and users are not being created, but can not understand where I made a mistake... APP ID and JS ID cleared for secrecy :)
HTML code has a form and the submit button, form has an ID = registration, button's id = registerForm
Tried to find this problem on the internet and found many variations of submitting the form, but neither helped :(
Parse.initialize("", "");
var currentUser = Parse.User.current();
if(curentUser) {
alert("Already logged in");
}
$(document).ready(function () {
$('#registerForm').click(function(){
$('#registration').submit(function() {
var username = $("#username").val();
var password = $("#password").val();
var email = $("#email").val();
//validation
var errors = "";
if(username === "") errors += "Username required.<br/>";
if(password === "") errors += "Password required.<br/>";
if(email === "") errors += "Email required.<br/>";
if(errors !== "") {
alert("Error!")
return;
}
var user = new Parse.User();
user.set("username", username);
user.set("password", password);
user.set("email", email);
user.signUp(null, {
success: function(user) {
alert("Yay!");
},
error: function(user, error) {
alert("Error: " + error.code + " " + error.message);
}
});
});
});
});
Upvotes: 1
Views: 952
Reputation: 97
And finally it worked with the code from a neighbor thread: parse.com & java script- upload image for user and show the image
I'm sorry to its author, I accidentally used your parse.initialize ID's, feel free to delete that fancy Tom Hardy with my email :D
Code that worked for me (and I changed in my html-code <button></button>
to <input type="submit">
) :
Parse.initialize("blabla", "blablatwo");
$(document).ready(function(){
$(document).on("submit", "#registration", function(e) {
e.preventDefault();
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
var email = document.getElementById('email').value;
var user = new Parse.User();
user.set("username", username);
user.set("email", email);
user.set("password", password);
var fileUploadControl = $("#profilephoto")[0];
if (fileUploadControl.files.length > 0) {
var file = fileUploadControl.files[0];
var name = "photo.png";
var parseFile = new Parse.File(name, file);
parseFile.save().then(function(parseFile) {
var url = parseFile.url();
user.set("image", url);
user.signUp();
},
function(error) {
alert("Error: " + error.code + " " + error.message);
});
};
});
});
Upvotes: 1