Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php文件_put_contents()替换'+';有空间_Php_Html_Ajax - Fatal编程技术网

php文件_put_contents()替换'+';有空间

php文件_put_contents()替换'+';有空间,php,html,ajax,Php,Html,Ajax,我试图使用php函数file\u put\u contents()和ajax一起覆盖一个文件,但在写入文件时,加号(+)被空格替换。下面是代码片段 <html> <head></head> <body> <div>inline editor will change this text</div> <button type="button" onclick="loadDoc

我试图使用php函数
file\u put\u contents()
和ajax一起覆盖一个文件,但在写入文件时,加号(+)被空格替换。下面是代码片段

<html>
    <head></head>
    <body>
        <div>inline editor will change this text</div>
        <button type="button" onclick="loadDoc()">save changes</button>
    <script>
        function loadDoc() {
            var html = document.getElementsByTagName('html')[0];
            var xhttp = new XMLHttpRequest();
            xhttp.open("POST", "overwrite.php", true);
            xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhttp.send("html="+html.outerHTML);  
            <!--the + here is replace with a space-->
        }
    </script>
    </body>
</html>
实际上,有两件事出了问题

1) 。+符号将被一个空格替换(由于@deceze@Justinas的同步回答而得以解决)

2) 。在head标记中添加了样式标记(仍然未解决且非常烦人)

如果能发现这里到底发生了什么,那就太好了,也许我可以修改代码来修复它

我很清楚允许用户修改内容,然后将其直接写入文件的安全风险,我只是在这里进行试验

感谢您在means space中的
+
!在将内容发送到服务器之前,您需要对其进行正确的url编码:

xhttp.send("html=" + encodeURIComponent(html.outerHTML));  
  • 您正在通过
    GET
    发送纯文本。在URL中,
    +
    表示空格,因此当PHP读取URL字符串时,它会自动对其进行URL解码,并且您的
    +
    将替换为空格。使用
    xhttp.send('html='+encodeURIComponent(html.outerHTML))

  • 您是否使用任何框架或任何其他自动化系统来自动附加样式


  • application/x-www-form-urlencoded将把+改为空格。您可以在覆盖中尝试urldecode($var)。php@Undefined_variable不,那是a)把它固定在错误的一端,b)你会对数据进行双重解码,这只会让事情变得更糟。嗨,谢谢你的回答,它工作起来很有魅力!!不,我没有使用任何框架,只是简单的php,我怀疑是outerHTML添加了这个?如果你能帮助我使用样式标记自动附加部分,你会比其他答案有优势;答案是P+1though@lensemagnifique在您提供的代码中,我看不出原因#2@lensemagnifique我不认为这都是你的代码,因为
    内联编辑器
    。它可能正在将其CSS添加到标题中。否则你将看不到编辑器的任何元素。我本来打算在将来添加ckeditor,但还没有添加,但公平地说,这解决了问题。嗨,谢谢你的回答,它工作得很有魅力!!我还可以看看为什么要添加样式标签吗?我没有足够的信息来帮你弄清楚。
    xhttp.send("html=" + encodeURIComponent(html.outerHTML));