将文本区域的内容保存到文件并从PHP服务器页面加载
Save content of textarea to file and load from PHP server page
如何使用ajax将html页面的文本区域的内容保存到文件中并将其加载到PHP服务器页面中(没有JQuery)?Html页面将是这样的:
<html>
<body>
<textarea id="editor"></textarea>
</body>
</html>
1.这将为您提供应用程序的结构,请注意此代码尚未经过测试。
服务器.php
<html>
<script>
var baseUrl="service.php";
function submitFormAjax()
{
var xmlhttp= window.XMLHttpRequest ?
new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
alert(xmlhttp.responseText); // Here is the response
}
var data = document.getElementById('editor').value;
xmlhttp.open("POST",baseUrl,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("action=write&data=" + data);
}
function readDataAjax()
{
var xmlhttp= window.XMLHttpRequest ?
new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
document.getElementById('editor').value=xmlhttp.responseText;
}
xmlhttp.open("POST",baseUrl,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("action=read");
}
</script>
<body onload="readDataAjax()">
<form method="post">
<textarea id="editor" name="editor"></textarea>
<button onClick="submitFormAjax();return false;">Submit</button>
</form>
</body>
</html>
<?php
$fileName="newfile.txt";
if(isset($_POST['action']))
{
switch($_POST['action'])
{
case 'read';
echo file_get_contents($fileName);
break;
case 'write';
if( isset($_POST['data']))
{
$myfile = fopen($fileName, "w") or die("Unable to open file!");
$txt = $_POST['data'];
fwrite($myfile, $txt);
fclose($myfile);
echo "file successfully saved";
}
break;
}
}
?>
?>
相关文章:
- 通过ajax将坐标传递到php服务器端,并在处理后检索到javascript
- 如何使用Javascript将空数组发送到PHP服务器
- 将通过 AJAX 上传的二进制数据保存在 PHP 服务器上
- 无法处理 PHP 服务器中由 FormData 发送的数据
- Fetch Api无法从PHP服务器获取会话
- 如何在等待php客户端脚本的回复时终止php服务器脚本
- javascript客户端时间到php服务器时间
- PHP 服务器无法使用 ajax 接收 javascript 对象
- 与Node.js服务器和PHP服务器之间的差异之间的理论混淆
- 如何将文件从jQuery上传到php服务器
- php 服务器端计时器 独立于客户端浏览器
- XMLHttp请求将 XML 发送到 PHP 服务器
- 如何上传 XML 文件并发送到 PHP 服务器
- 推送器 PHP 服务器不工作
- 将文本区域的内容保存到文件并从PHP服务器页面加载
- 如何在我的本地主机 php 服务器上安装更少
- 脚本未将输入发布到PHP服务器脚本
- JavaScript-如何获取PHP服务器的正确时间
- ajax - 将数据作为 JSON 发送到 PHP 服务器并接收响应
- 从php服务器读取json