【发布时间】:2013-11-07 20:51:18
【问题描述】:
在我的网站中,我必须阅读、编辑和保存一些数据。我是这样执行的:
- 使用 PHP 加载一个名为
database.txt的文本文件(存储在服务器中),该文件位于具有id="testo"的文本区域中 - 调用
importa();,这是一个javascript函数,可以编辑testo中的文本 - 我将textarea的内容保存在
database.txt中
这是我用来在 textarea 中加载文本的代码:
<?php
$myFile = "database.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo '<textarea id="testo" style="width:100%;display:block;">'.$theData.'</textarea>';
?>
我有一个按钮Save 调用importa();。
<input type="button" id="salvauser" value="Save" style="width:100%" onclick="document.getElementById('settings').style.display='none';importa();" />
现在我有了从 importa 编辑的文本区域,我必须将其文本保存到 database.txt。我写了这个函数:
<?php
$testo = $_POST['testo']."\r\n";
$documento ="database.txt";
$identificatore = fopen($documento, "a");
if (!fwrite($identificatore, $testo)){
echo"?PHP ERROR: Cannot save the file. Script not loaded.";
exit;
}
else {
fclose($identificatore);
header('Location: http://escaperope.altervista.org/testsito/index.php');
}
?>
它保存了textarea的内容,但是我在调用importa();之后不知道如何调用这个PHP脚本(我是PHP新手)。你有什么建议吗?
这里可以看到importa();
function addText(elId,text) {
document.getElementById(elId).value += text;
}
//addText allows me to add the text inside the textarea
function importa() {
addText("testo",document.getElementById("nome2").value + ":" + document.getElementById("fc2").value+":" + document.getElementById("poke1").value + ":" + document.getElementById("poke2").value + ":" + document.getElementById("poke3").value + "tipo");
}
【问题讨论】:
-
请记住 PHP 是服务器端,而 javascript 是客户端,所以您可能需要为此使用 ajax,请同时发布
importa()函数。 -
完成,我已经添加了函数 importa();
-
函数 savetext() { jQuery.ajax({ type: "POST",url: "action.php", });
-
自己试试看!但我会说它缺少一些东西。
标签: javascript php jquery html