How to pass original innerHTML to php?

The user selects the text and replaces it with an id = 'NewLink' tag, when he finishes describing the word or phrase, the server will receive the innerHTML of the content of the 'StartId' div. What I need to overlook is that the user is not modifying the client side HTML in the browser. How can i do this?

  function GetSelectedText () {
   var sel = window.getSelection();  
            
   if( sel.toString().length != 0 && sel.getRangeAt) {
   $('#NewLink').replaceWith($('#NewLink').html());

     var rng, se, err, errt, patt;
     errt = 0;
     try {
	   
     se=$('<span style="background-color: #3366CC;" id="NewLink">')[0];

        rng=sel.getRangeAt(sel.rangeCount-1);
        rng.surroundContents(se);
        rng.selectNode(document.getElementById("NewLink"));
         } catch (err) { errt = 1;}
    
        if(errt != 1)  
        {
        patt=new RegExp("</?a.*?>","g");
        if (patt.test($("#NewLink").html()) ||$("#NewLink").closest("a").length||!$("#NewLink").text().replace(/\s/g,"").length) 
        {$("#NewLink").replaceWith($("#NewLink").html()); }
        else { rng.collapse(!0);}
        }
    }
    var StartId = document.getElementById("StartId").innerHTML;
    $.post('site.com', {htmlInner:StartId}, function(data) {
	   alert(data);
		  });
      
    } 
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="StartId" onmouseup="GetSelectedText ()">
Carnotaurus, a large theropod dinosaur, lived during the Late Cretaceous period. Known from a single well-preserved skeleton found in Argentina, it is a member of the Abelisauridae family<a href="#">and one of the best-understood theropods from the Southern Hemisphere. <br>Carnotaurus (derived from Latin for "meat-eating bull") <br> had thick horns above the eyes, and a very deep skull on a muscular neck.</a> <br>It was a lightly built, bipedal predator, 8 to 9 m (26.2 to 29.5 ft) long, weighing at least 1.35 metric tons (1.49 short tons). It had small, vestigial forelimbs and long and slender hindlimbs. Preserved skin impressions show a mosaic of small scales interrupted by large bumps that lined the sides of the animal. </div>
<div id="EndId"></div>
      

Run codeHide result


+3


source to share


1 answer


change your code $.post

to this:

$.post('site.com', '{"htmlInner":"' + StartId + '"}', function(data) {
       alert(data);
    });          
} 

      

your parameter is not serialized correctly.



if yours htmlInner

is an integer, you can change this setting like this:

'{"htmlInner": '+ StartId + '}'

0


source







All Articles