Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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
Javascript 在服务器上编辑并保存文件(PHP) 我有一个页面,它将C++文件的内容显示为TeXTaRea/Cord>,并且我需要能够使用脚本保存它的内容。(C++文件不必被配置为刚刚保存)。_Javascript_Php_Html - Fatal编程技术网

Javascript 在服务器上编辑并保存文件(PHP) 我有一个页面,它将C++文件的内容显示为TeXTaRea/Cord>,并且我需要能够使用脚本保存它的内容。(C++文件不必被配置为刚刚保存)。

Javascript 在服务器上编辑并保存文件(PHP) 我有一个页面,它将C++文件的内容显示为TeXTaRea/Cord>,并且我需要能够使用脚本保存它的内容。(C++文件不必被配置为刚刚保存)。,javascript,php,html,Javascript,Php,Html,我正在使用一个PHP脚本从文件中加载代码,并将其显示在textarea上。如何将内容发送回脚本并将其保存到同一文件或具有新名称的文件 PHP,HTML文件: 拯救 脚本: function savefiles() { var contentArea = document.getElementsById('cpp_content'); var cpp_content = contentArea.value; var request = new XMLHttpRequ

我正在使用一个PHP脚本从文件中加载代码,并将其显示在
textarea
上。如何将内容发送回脚本并将其保存到同一文件或具有新名称的文件

PHP,HTML文件:




拯救
脚本:

function savefiles() {
    var contentArea = document.getElementsById('cpp_content');
    var cpp_content = contentArea.value;
    var request = new XMLHttpRequest();
    request.open('POST', '/php/save_contents.php', true);
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

    request.onload = function() {
        if (this.status >= 200 && this.status < 400) {
            console.log("Success");
            var resp = this.response;
        } else {
        alert ("Target server reached, but it returned an error" );
        }
    };
    request.onerror = function() {
      // There was a connection error of some sort
    };
    request.send(cpp_content);
}
函数savefiles(){
var contentArea=document.getElementsById('cpp_content');
var cpp_content=contentArea.value;
var request=new XMLHttpRequest();
open('POST','/php/save_contents.php',true);
setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=UTF-8');
request.onload=函数(){
如果(this.status>=200&&this.status<400){
控制台日志(“成功”);
var resp=此响应;
}否则{
警报(“已到达目标服务器,但返回错误”);
}
};
request.onerror=函数(){
//出现了某种连接错误
};
请求发送(cpp_内容);
}
PHP文件:


我希望文本文件的C++文件在下面的代码中得到内容的保存。p>

ig@WookieeTyler

另一种选择是通过XMLHttpRequest将内容发送到单独的PHP文件。这样,保存时不必重新加载页面。大概是这样的:

var request = new XMLHttpRequest();
request.open('POST', '/my/url/save_contents.php', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

request.onload = function() {
  if (this.status >= 200 && this.status < 400) {
    // Success!
    var resp = this.response;
  } else {
    // We reached our target server, but it returned an error

  }
};

request.onerror = function() {
  // There was a connection error of some sort
};

request.send('cpp_content=' + cpp_content);
var-request=new-XMLHttpRequest();
open('POST','/my/url/save_contents.php',true);
setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=UTF-8');
request.onload=函数(){
如果(this.status>=200&&this.status<400){
//成功!
var resp=此响应;
}否则{
//我们到达了目标服务器,但它返回了一个错误
}
};
request.onerror=函数(){
//出现了某种连接错误
};
请求发送('cpp\U内容='+cpp\U内容);

但是如果你使用这个,你也会被黑客攻击:)我明白你的意思,但它不会保存新内容。在save_files.php文件中,我将文件路径分配给$filename,并使用$cpp_content=$\u POST['cpp_content']获取新内容,然后使用“file_put_contents($filename,$cpp_content);”。我做错什么了吗?对。这是因为在我的示例中,我没有以key=value对的形式发送数据。尝试使用此
请求更改las行。发送('cpp_content='+cpp_content)我继续在我的本地主机上做了一个简单的页面,它成功了,所以谢谢你。然而,它不会在我正在处理的实际页面上工作。我不明白为什么,但非常感谢,因为现在我知道它确实有效。
var request = new XMLHttpRequest();
request.open('POST', '/my/url/save_contents.php', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

request.onload = function() {
  if (this.status >= 200 && this.status < 400) {
    // Success!
    var resp = this.response;
  } else {
    // We reached our target server, but it returned an error

  }
};

request.onerror = function() {
  // There was a connection error of some sort
};

request.send('cpp_content=' + cpp_content);