簡體   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