I need help with uploading a photo (along with other inserted data in editor) but only when submit button is clicked. I've searched forum, and googled but no luck :(
The code I'm using is working for uploading image and saving text to database, but it's uploading image instantly when I add it to editor. This is not desired behavior for me, 'couse if user add image to editor and then decide to close tab/close browser or go to another address, image will be stored on server - so I would like someone to help me to upload image only when submitt button is pressed (until then, it will be there only as preview). Here is my code:
$('#summernote').summernote({
//placeholder: 'your Message',
height: 200,
toolbar: [
['style', ['style']],
['font', ['bold', 'italic', 'underline', 'clear']],
['fontname', ['fontname']],
['color', ['color']],
['para', ['ul', 'ol', 'paragraph']],
['height', ['height']],
['table', ['table']],
['insert', ['link', 'picture', 'hr']],
['view', ['fullscreen', 'codeview', 'help']],
['save-button', ['save']]
],
callbacks : {
onImageUpload: function(image) {
uploadImage(image[0]);
}
}
});
function uploadImage(image) {
var slika = new FormData();
slika.append("image",image);
$.ajax ({
data: slika,
type: "POST",
url: "url - upload image script",// this file uploads the picture and
// returns a chain containing the path
cache: false,
contentType: false,
processData: false,
success: function(url) {
var image = url;
$('#summernote').summernote("insertImage", image);
console.log(slika);
},
error: function(data) {
console.log(slika);
}
});
}
$(".note-save-button").addClass("pull-right");
$(function(){
$('#addit_dtls_form').submit(function(event){
var input_content = $('#summernote').summernote('code');
var is_empty = $("#is_empty").val();
var location_id = $("#location_id").val();;
//event.preventDefault();
$.ajax({
url: 'url - store text to database',
type: 'post',
data: {
'input_content' : input_content,
'is_empty' : is_empty,
'location_id' : location_id,
},
success: function(response){
$.smallBox({
title : "USPEŠNO!",
content : "Sadržaj je uspešno snimljen!",
color : "#7DC27D",
timeout: 4000,
icon : "fa fa-check"
});
//console.log(input_content);
}
});
});
});
Hope someone can help me, or there is some example code someone can point me to. Tnx in advance!