Answer the question
In order to leave comments, you need to log in
How to correctly pass strings to a function?
Hello, please help me understand.
There are two forms (.user-create-form and .user-edit-form) and when saving data (on submit), I want to display messages in a block (#user-action-message). For example, when creating a user, the message "A new user has been created", and when editing, the message "User data has been updated". Tried to use the following code but it throws an error. I think I am incorrectly passing the string to the saveForm function.
ERROR:
TypeError: Argument 1 of FormData.constructor does not implement interface HTMLFormElement.
var saveForm = function (success_message) {
var form = $(this);
var formData = new FormData(form[0]);
$.ajax({
url: form.attr("action"),
data: formData,
type: form.attr("method"),
dataType: 'json',
success: function (data) {
if (data.form_is_valid) {
$("#users").html(data.html_users);
$("#user-action-message").html(success_message); <-- ПОКАЗАТЬ сообщения здесь
$("#modal-lg").modal("hide");
}
else {
$("#user-modal .modal-content").html(data.html_form);
}
}
});
return false;
};
$("#user-modal").on("submit", ".user-create-form", saveForm("Новый пользователь успешно создан!"));
$("#user-modal").on("submit", ".user-edit-form", saveForm("Данные пользователя успешно сохранены!"));
Answer the question
In order to leave comments, you need to log in
Everything is ok with the line.
Check what exactly you are passing in FormData.
Something like this: https://jsfiddle.net/z5jmmdpa/
The problem is somewhere in . Print to the console up to new FormData form[0]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question