title: Save the contents of manipulated div to a variable and pass to php file
body: <p>I have tried to use AJAX, but nothing I come up with seems to work correctly. I am creating a menu editor. I echo part of a file using php and manipulate it using javascript/jquery/ajax (I found the code for that here: <a href="http://www.prodevtips.com/2010/03/07/jquery-drag-and-drop-to-sort-tree/" rel="nofollow">http://www.prodevtips.com/2010/03/07/jquery-drag-and-drop-to-sort-tree/</a>). Now I need to get the edited contents of the div (which has an unordered list in it) I am echoing and save it to a variable so I can write it to the file again. I couldn't get that resource's code to work so I'm trying to come up with another solution. </p>
  
  <p>If there is a code I can put into the <code>$("#save").click(function(){ });</code> part of the javascript file, that would work, but the <code>.post</code> doesn't seem to want to work for me. If there is a way to initiate a php <code>preg_match</code> in an onclick, that would be the easiest.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>The code to get the file contents.</p>
  
  <pre><code>&lt;button id="save"&gt;Save&lt;/button&gt;
  &lt;div id="printOut"&gt;&lt;/div&gt;
  &lt;?php
      $header = file_get_contents('../../../yardworks/content_pages/header.html');
      preg_match('/&lt;div id="nav"&gt;(.*?)&lt;\/div&gt;/si', $header, $list);
      $tree = $list[0];
      echo $tree;
  ?&gt;
  </code></pre>
  
  <p>The code to process the new div and send to php file.</p>
  
  <pre><code>$("#save").click(function(){
      $.post('edit-menu-process.php', 
          {tree: $('#nav').html()}, 
          function(data){$("#printOut").html(data);}
      );
  });
  </code></pre>
  
  <p>Everything is working EXCEPT something about my encoding of the passed data is making it not read as html and just plaintext. How do I turn this back into html?
  EDIT: I was able to get this to work correctly. I'll make an attempt to switch this over to DOMDocument.</p>
  
  <pre><code>$path = '../../../yardworks/content_pages/header.html';
      $menu = htmlentities(stripslashes(utf8_encode($_POST['tree'])), ENT_QUOTES);
      $menu = str_replace("&amp;lt;", "&lt;", $menu);
      $menu = str_replace("&amp;gt;", "&gt;", $menu);
  
      $divmenu = '&lt;div id="nav"&gt;'.$menu.'&lt;/div&gt;';
  
      /* Search for div contents in $menu and save to variable */
      preg_match('/&lt;div id="nav"&gt;(.*?)&lt;\/div&gt;/si', $divmenu, $newmenu);
      $savemenu = $newmenu[0];
  
      /* Get file contents */
      $header = file_get_contents($path);
  
      /* Find placeholder div in user content and insert slider contents */
      $final = preg_replace('/&lt;div id="nav"&gt;(.*?)&lt;\/div&gt;/si', $savemenu, $header);
  
      /* Save content to original file */
      file_put_contents($path, $final);   
  ?&gt;
  Menu has been saved.
  </code></pre>