简体   繁体   中英

ajax response contains the html for the whole page instead of the content of text editor

i am working with ckeditor.I used ajax to send the content of the ckeditor using post method.My plan is to show the response in a div.But in response i get the html for the whole page enclodes by html tag instead of only the editor's content.What is going on here? How can i get only the editor's content

在此处输入图像描述

<?php  
if(isset($_POST) && !empty($_POST)){
echo $_POST['content'];
}
?>

<html>
<head>
  <script  src='ckeditor/ckeditor.js'></script>
</head>
<body>
 <form action='test.php' method='post'>
            <textarea name="editor1" id="editor1" rows="10" cols="80">
                This is my textarea to be replaced with CKEditor.
            </textarea>
            <input type='button' value='submit' onClick='lol(event);'>
    </form>
    <div id='content' style='border:2px solid black;'>

    </div>
            <script>

CKEDITOR.replace( 'editor1');
function lol(event){
    var xhr=new XMLHttpRequest();

    xhr.onreadystatechange=function(){
       if(xhr.readyState==4 && xhr.status==200){
           //document.getElementById('content').innerHTML=xhr.response;  
           alert(xhr.responseText);
       }
    }
    var value=CKEDITOR.instances.editor1.getData();
    var params = "content="+value;
    xhr.open('POST','test.php',true);
    //xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send(params);
    //alert(value);
}

            </script>


</body>
</html>

When You use echo , the response is sent and then the html is sent. If you want to stop after echo , use die(); :

<?php  
  if(isset($_POST) && !empty($_POST)) {
    die($_POST['content']);
  }
?>

Also, you should check that content is set:

<?php  
  if(isset($_POST) && !empty($_POST) && isset($_POST['content'])) {
    die($_POST['content']);
  }
?>

Try this solution by adding variable to Javascript variable which called params and this variable to check if already these items opened or not and prevent main items to not open more than 1 time even if u called your page by ajax, to be clear try l8ike that var params = "content="+value+"isopepend=1"; then use php if statement like if there is no required variable then print your main items and this will only happen one time when u will visit the page directly with no ajax request

<?PHP
    if(!$_POST['isopepend'])
    {
    echo "home elements";
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM