Andrew Andrew - 1 month ago 6
Ajax Question

How to save current DOM with Ajax, jQuery and PhP?

I have edited (appended some of my div's)in my page, now, I need to save all of it, and of course to replace old one.

DOM body example:

<body>
<div id="save_dom">Save</div>

<div class="box">111</div>
<div class="box">222</div>

<script type="text/javascript" >
jQuery(document).ready(function($) {

$(document).on('click', '#save_dom', function(event) {
$.ajax({
url: "/saver.php",
type: "POST",
data: {
// chose data to send
}, success: function(response) {
// do things if successed
}
});
});

});
</script>

</body>


saver.php file example:

<?php
$data = $_GET['data'];
$file = $_SERVER['DOCUMENT_ROOT'];
file_put_contents($file, $data);
echo'Saved!';


Something in this way.

How to save current dom via Ajax, jQuery, PhP?

Answer

i don't think it's good to save the DOM in DB..

you can post the dom with Ajax by getting the html of the body

$("body").html();

Example

jQuery(document).ready(function($) {
  $(document).on('click', '#save_dom', function(event) {
    $.ajax({
      url: "/saver.php",
      type: "POST",
      data: {
      Dom : $("body").html()
      }, success: function(response) {
      // do things if successed
      }
    });
  });

});