<button id="openBtn">选择文件</button> <button id = "saveBtn">保存</button> <br><br> <textarea id="fileContent" cols="80" rows="30"></textarea> <script> $(document).ready(function(){ $("#openBtn").click(function(){ $.ajax({ url:"ntp.php", type:"GET", success:function(response){ $("#fileContent").val(response); } }); }); $("#saveBtn").click(function(){ var fileContent = $("#fileContent").val(); $.ajax({ url:"ntp.php", type:"POST", data:{content:fileContent}, success:function(response){ alert("File saved successfully!"); } }); }); }); 调用以下代码$file_path = "/etc/ntp.conf"; $file_content = file_get_contents($file_path); $file_content1 = $_POST['content']; file_put_contents($file_path,$file_content); php文件不需要echo $file_content html文件就可以将$file_content的值输入到文本框中
时间: 2024-01-28 20:03:46 浏览: 34
是的,如果你的 PHP 代码确保 `$file_content` 变量中包含了正确的文件内容,那么 HTML 页面中的 JavaScript 代码就可以通过 AJAX 请求将文件内容设置到文本框中,而不需要 PHP 文件返回任何值。
以下是修改后的 PHP 代码,它会将从文件中读取到的内容写回到同一个文件中:
```php
$file_path = "/etc/ntp.conf";
$file_content = file_get_contents($file_path);
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$file_content1 = $_POST["content"];
file_put_contents($file_path, $file_content1);
exit();
}
```
请注意,这里的 `exit()` 函数用于终止 PHP 脚本的执行,以确保不会输出任何多余的内容。同时,如果你使用了 AJAX 来发送 POST 请求,那么你也不需要在 PHP 文件中使用 `echo` 函数来返回任何值。
相关问题
<h1>编辑NTP配置文件</h1> <hr> <button id = "openBtn">打开文件</button> <button id = "saveBtn">保存</button> <br><br> <textarea id="fileContent" cols="80" rows="30"></textarea> <script> $("#openBtn").click(function(){ $.ajax({ url:"ntp1.php", type:"GET", success:function(response){ var fc = response; $("#fileContent").val(fc); } }); }); $("#saveBtn").click(function(){ var fileContent = $("#fileContent").val(); $.ajax({ url:"ntp1.php", type:"POST", data:{content:fileContent}, success:function(response){ alert("File saved successfully!"); } }); }); </script> 优化代码
对于这段代码,可以将 HTML 和 JavaScript 部分分离,提高代码可读性和维护性。同时,也可以将 Ajax 请求的 url 和 type 参数提取出来,方便维护。
HTML 部分:
```
<h1>编辑NTP配置文件</h1>
<hr>
<button id="openBtn">打开文件</button>
<button id="saveBtn">保存</button>
<br><br>
<textarea id="fileContent" cols="80" rows="30"></textarea>
```
JavaScript 部分:
```
var url = "ntp1.php";
var type = "GET";
function getFileContent() {
$.ajax({
url: url,
type: type,
success: function(response) {
var fc = response;
$("#fileContent").val(fc);
}
});
}
function saveFile() {
var fileContent = $("#fileContent").val();
$.ajax({
url: url,
type: "POST",
data: { content: fileContent },
success: function(response) {
alert("File saved successfully!");
}
});
}
$(document).ready(function() {
$("#openBtn").click(function() {
getFileContent();
});
$("#saveBtn").click(function() {
saveFile();
});
});
```
优化后的代码将 Ajax 请求的 url 和 type 参数提取出来,方便维护。同时,使用 `$(document).ready()` 函数来确保页面加载完成后再执行 JavaScript 代码,避免出现未定义的错误。
<h1>编辑NTP配置文件</h1> <hr> <button id="openBtn">打开文件</button> <button id = "saveBtn">保存</button> <br><br> <textarea id="fileContent" cols="80" rows="30"></textarea> <script> $(document).ready(function(){ $("#openBtn").click(function(){ $.ajax({ url:"ntp1.php", type:"GET", success:function(response){ $("#fileContent").val(response); } }); }); $("#saveBtn").click(function(){ var fileContent = $("#fileContent").val(); $.ajax({ url:"ntp1.php", type:"POST", data:{content:fileContent}, success:function(response){ alert("File saved successfully!"); } }); }); }); </script> 优化代码
可以将 JavaScript 的代码提取出来,放在外部的 js 文件中,同时将 jQuery 的 $ 符号替换成 jQuery,以避免和其他 JavaScript 库的冲突。代码如下:
HTML 文件:
```
<h1>编辑NTP配置文件</h1>
<hr>
<button id="openBtn">打开文件</button>
<button id="saveBtn">保存</button>
<br><br>
<textarea id="fileContent" cols="80" rows="30"></textarea>
<script src="js/main.js"></script>
```
JavaScript 文件(js/main.js):
```
jQuery(document).ready(function($) {
$("#openBtn").click(function() {
$.ajax({
url: "ntp1.php",
type: "GET",
success: function(response) {
$("#fileContent").val(response);
}
});
});
$("#saveBtn").click(function() {
var fileContent = $("#fileContent").val();
$.ajax({
url: "ntp1.php",
type: "POST",
data: {
content: fileContent
},
success: function(response) {
alert("File saved successfully!");
}
});
});
});
```
此外,为了提高代码的可读性和可维护性,可以将 PHP 文件中的 GET 和 POST 请求分别处理,分别写成不同的函数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)