I used below function to send my form data with ajax:
$(function() {
$('#sendButton').click(function(e) {
e.preventDefault();
var temp = $("#backupSubmit").serialize();
$.ajax({
type: "POST",
data: temp,
url: 'backup/',
success: function(data) {
$("#response").html('<p>{% trans "you can download it from here:" %}'+'<a href="'+data+'">{% trans "download" %}</a></p>');
$("#response").show();
}
});
});
});
now i want to use this function:
$("#sendButton").click(function(e){
e.preventDefault();
save_data();
if(save_data()) {
alert('Saved!');
} else {
alert('Failed!');
}
});
function save_data() {
$.post("backup/", {
csrfmiddlewaretoken: $("input[name='id_csrfmiddlewaretoken").val(),
???? backup_from: $("#backupSubmit").serialize()
},
function(data) {
data = json_parse(data);
if(data.status=="success") {
return true;
} else {
console.log("status: "+data.status)
console.log("error: "+data.error)
console.log("POST DATA: "+data.data)
$.each(data.data, function(i, n){
console.log(">"+i+": "+n);
});
return false;
}
},
"json"
);
}
but in view, it seems that form is not valid! and this is my form:
BACKUP_CHOICES = (('systemSettings',_("systemSettings")),
('ruleSet',_("ruleSet")))
class backupForm(forms.Form):
backup_from = forms.MultipleChoiceField(widget=CheckboxSelectMultiple, label=_("backup from"), required=True,
choices=BACKUP_CHOICES, error_messages={'required': _("Please choose at least one option")})
It sends data with post, and i don't have any validation check, except requirement. i don't know why the form is not valid? Is it correct to send form data like the above (next to ????)? thanks for your help: