1

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);
        }
    });
});
    });
});
2
  • Are you seeing username, passwrod values coming through? Commented Jan 20, 2014 at 11:53
  • I think not. Tried to add this element to code but didn't get an alert: $(document).ready(function(){ $("#registerForm").click(function(){ value = $("#username").attr('value'); alert(value); }); }); Commented Jan 20, 2014 at 12:07

1 Answer 1

1

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);
      });
  };
});
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.