I have an iframe which is editable to be used as post input for a blog.
I’m using jQuery to retrieve the html from the body of the iframe:
function sharePost (feedNumber)
{
var postBox = $("#ifrNewPost" + feedNumber).contents(); // Gets iframe content
var postContent = $('body', postBox).html(); // Gets iframe body
var params = { post: encodeURI(postContent) };
// Send to server
$.ajax({
type:'POST',
url: 'script/post.php',
data: params,
success: function(response) {
$(document).append(response);
}
});
}
Nearly works perfectly, my problem is that when I paste something into the iframe, there seems to be a newline added to the start of what I paste, then when submitted, the script is failing with:
“Uncaught SyntaxError: Unexpected token ILLEGAL”
I have tried encodeURI and encodeURIComponent to no avail. When I view the code of the page after pasting there are no apparent changes to what I paste, it just appears to have a new line added before it. Any solutions on how to send the HTML safely through to my php script?
The problem was with what the php code was returning after all!
Simply running a regex to change any newlines to br’s did the job!