在JS函数调用后加载php函数

时间:2013-11-07 20:51:18

标签: javascript php jquery html

在我的网站中,我必须阅读,编辑和保存一些数据。我用这种方式表演:

  1. 在PHP中加载一个名为database.txt的文本文件(存储在服务器中),文本文件位于id="testo"
  2. 调用importa();这是一个编辑testo
  3. 内的文字的javascript函数
  4. 我在database.txt
  5. 中保存了textarea的内容

    这是我用来在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脚本(我是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");
    }
    

1 个答案:

答案 0 :(得分:1)

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 source 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()&amp; JSON.stringify();

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

如果您有任何其他问题,请询问。

修改

我注意到你现在要在db.txt中存储你的用户和pokes ......

以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数组,其中包含创建精美显示函数所需的所有数据。