简体   繁体   中英

Load php function after JS function call

In my website I have to read, edit and save some datas. I perform it in this way:

  1. Load with PHP a text file called database.txt (stored in the server) in a textarea that has id="testo"
  2. Call importa(); that is a javascript function that edit the text inside testo
  3. I save content of the textarea inside database.txt

This is the code I use to load the text inside the 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>';
?>

I have a button Save that calls importa(); .

<input type="button" id="salvauser" value="Save" style="width:100%" onclick="document.getElementById('settings').style.display='none';importa();" />

Now I have the textarea that is edited from importa and I must save its text to database.txt . I wrote this function:

<?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');
}
?>

It saves the content of the textarea, but I don't know how to call this PHP script after calling importa(); (I am new with PHP). Do you have any suggestions?

Here you can see 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 way... Pure 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

after that you can execute addtext or whatever you want.

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);
}
?>

not tested ... but should work in modern browsers...

if you want to have more control over your database.. use JSON.parse() & 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

if you have any other questions just ask.

EDIT

i noticed now you want to store your users and pokes or whatever in the db.txt...

structure everything in a json way.. :

[
 {
  "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"}
  ]
 },
]

this is easely done with creating a javascript array...

then convert it to a text string to store into database.txt

using fd.append('data',JSON.stringify(javascriptArray)) inside the salva function

to read the text

inside the visualizza function

use JSON.parse(this.response) to transform your text back to a javascript array which contains all the data you need to create a nice display function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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