繁体   English   中英

JS函数调用后加载php函数

[英]Load php function after JS function call

在我的网站上,我必须阅读,编辑和保存一些数据。 我以这种方式执行:

  1. 用PHP在文本区域中具有id="testo"名为database.txt的文本文件(存储在服务器中)加载
  2. 调用importa(); 这是一个JavaScript函数,可编辑testo的文本
  3. 我将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编辑的textarea,我必须将其文本保存到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脚本importa(); (我是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");
}

html5方式...纯Javascript ...

index.html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
<script src="rw.js"></script>
</head>
<body>
<textarea id="testo"></textarea>
<button id="salva">Salva</button>
</body>
</html>

rw.js

function x(a,b,e,d,c){ // Url,callback,method,formdata or {key:val},placeholder
 c=new XMLHttpRequest;
 c.open(e||'get',a);
 c.onload=b;
 c.onerror=error;
 c.send(d||null)
}
function salva(){
 var fd=new FormData();
 fd.append('data',document.getElementById('testo').value);
 x('save.php',controllo,'post',fd);
}
function controllo(){
 if(this.response=='ok'){
  alert(this.response);//ok
  leggi();
 }else{
  alert(this.response);//errore
 }
}
function leggi(){
 x('database.txt',visualizza);
}
function visualizza(){
 document.getElementById('testo').value=this.response;
}
window.onload=function(){
 document.getElementById('salva').addEventListener('click',salva,false);
 leggi();
}

ajax源https://stackoverflow.com/a/18309057/2450730

之后,您可以执行addtext或任何您想要的。

save.php

<?php
if($_POST['data']){
 $fh=fopen('database.txt','w') or die("non riesco ad aprire il file");
 echo (fwrite($fh,$_POST['data']))?'ok':'errore';
 fclose($fh);
}
?>

未经测试...但是应该可以在现代浏览器中运行...

如果要对数据库有更多控制,请使用JSON.parse()和JSON.stringify();。

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects / JSON /字符串化

如果您还有其他问题,请问。

编辑

我注意到现在您想存储您的用户和戳或db.txt中的任何内容...

以json的方式构造一切。.:

[
 {
  "nome":"pippo",
  "pokes":[
   {"time":05505151,"poketxt":"lol","type":"msg"},
   {"time":05505152,"poketxt":"lol2","type":"boh"}
  ]
 },
 {
  "nome":"ciccio",
  "pokes":[
   {"time":05505155,"poketxt":"lolx","type":"msg"},
   {"time":05505156,"poketxt":"lolxx","type":"boh2"}
  ]
 },
]

这很容易通过创建javascript数组来完成...

然后将其转换为文本字符串以存储到database.txt

使用fd.append('data',JSON.stringify(javascriptArray))内部salva功能

阅读文字

visualizza函数内部

使用JSON.parse(this.response)将您的文本转换回javascript数组,其中包含创建漂亮的显示函数所需的所有数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM