In my website I have to read, edit and save some datas. I perform it in this way:
- Load with PHP a text file called
database.txt
(stored in the server) in a textarea that hasid="testo"
- Call
importa();
that is a javascript function that edit the text insidetesto
- 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");
}