1

Is this possible? I've been searching and have not been successful in finding any solid answers. I've tried submitting the user registration form with ajax and it just returns the expected html page. I hope I'm not just missing some parameter.

** edit **
figured out that you can submit, if successful you will get the expected response. If their are errors you will get undesirable response back. It a normal Expression Engine error page

1 Answer 1

4

This works for logging into ExpressionEngine with ajax and showing the appropriate error, you would just modify the code where it checks for the error messages it returns to display to the user. You will need more conditions for the required fields.

function displayAjaxMessage(message, response_id)
{
    if(typeof(response_id)==="undefined") response_id = "#form-message";
    $(response_id).html(message);
    $(response_id).show();
}

// using the jquery validate library 
$("#register-form").validate({
    rules: {
        username: {
            required: true,
            email: true
        },
        password: {
            required: true,
            minlength: 5
        },
        password_confirm: {
            required: true,
            minlength: 5,
            equalTo: "#register_confirm_password"
        },
    },

    submitHandler: function() {
        $.ajax({
            url: $("#register-form").attr("action"),
            data: $("#register-form").serialize(),
            dataType: "html",
            type: 'POST',

            error: function (jxhr, msg, err) 
            {
                $('#register-form').append('<li style="color:red">' + msg + '</li>');
            },

            success: function(html, textStatus, jqXHR) 
            {
                if (html.match(/<title>Error<\/title>/)) 
                {
                    var error = $(html).find('ul li:first').text(); 

                    if(error == "The username you chose is not available")
                    {
                        displayAjaxMessage("This email address is already registered",  "#register-form-message");
                    } else {
                        displayAjaxMessage(error,  "#register-form-message");
                    }
                } else {
                    $("#register-form").hide();
                    displayAjaxMessage("Thanks! You can now login!",  "#register-form-message");
                }
            }
        });
    } 
});

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.