Submit Div content via jQuery

I would really like to take all the html in a div (contains many other divs, form elements, etc.), urlencode (maybe), and then make an ajax message to the server side handler so that I can save the exact state of what I see user.

Then I would like to take this line, which I saved in the database, and send it back in response to the ajax call in the future.

Should I use something like the following?

var contents = escape($('#myDiv').html());

Then do the ajax post operation for the server side handler? Would it make sure that I didn't pass any special characters that would ruin the ajax call?

Any pointers are appreciated.

+3
source share
2

, POST. GET, URI, , .

, :

$.post('/my_handler', {'html': $('#myDiv').html()}. function(data){
     //do something with data     
});

script /my_handler post html . , PHP, $_POST['html'].

+2

:

function htmlEncode () {         return $('< div/" > '). text (value).html();       }

function htmlDecode () {         return $('< div/" > '). html (value).text();       }

var content = htmlEncode ($ ('# myDiv'). html());

+1
source

Source: https://habr.com/ru/post/1751789/


All Articles