I'm not a pro with ajax and javascript in general so I have to ask. I have a form with an editor inside (summernote editor)
<div id="summernote">summernote text</div>
Then I send form data throught this ajax function:
$( '#form-create-project' ).submit( function( e ) {
$.ajax({
url: 'create_project.php',
type: 'POST',
data: new FormData( this ),
processData: false,
contentType: false
});
e.preventDefault();
});
But I can't find a way to pass the summernote value too, I've tried with that:
var content = $('textarea[name="content"]').html($('#summernote').code());
but I don't know where to place that inside the js above. When I try to insert it the form on submit refresh the page instead of execute ajax, so I think I placed it in the wrong place.
Someone can help me?
Can you try to escape the html code before to submit:
For Example:
txtText.summernote({ height: 400, lang: 'es-ES'});
$.fn.htmlEscape = function () {
return this.val().trim()
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
},
// I needed the opposite function today, so adding here too:
$.fn.htmlUnescape = function () {
return this.val().trim()
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&');
},
function getDataForm() {
var data = new FormData();
if (Files.ImageSelection.getAction() === "choose") {
data.append("ImagePath", ImagePath);
}
else {
var files = Files.ImageSelection.getSelectedFile().get(0).files;
if (files.length > 0) {
data.append("File", files[0]);
}
data.append("ImagePath", "");
}
data.append("Id", SectionId);
data.append("Title", txtTitle.val().trim());
data.append("Text", txtText.htmlEscape());
data.append("TypeId", cboType.val());
return data;
}
You could add the value simply to formData
using append()
beforre the sent :
formData.append("summernote", $('#summernote').text() );
Full code like :
var formData = new FormData( this );
formData.append("summernote", $('#summernote').text() );
$.ajax({
url: 'create_project.php',
type: 'POST',
data: formData,
processData: false,
contentType: false
});
Hope this helps.