JS函数调用后加载php函数 [英] Load php function after JS function call

查看:90
本文介绍了JS函数调用后加载php函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  1. 用PHP加载一个名为 database.txt (存储在服务器中)放在一个textarea中,该textarea包含 id =testo

  2. 调用 importa (); 这是一个JavaScript函数,用于编辑 testo
  3. 中的文本
  4. textarea在 database.txt中

这是我用来加载textarea中的文本:

 <?php 
$ myFile =database.txt;
$ fh = fopen($ myFile,'r');
$ theData = fread($ fh,filesize($ myFile));
fclose($ fh);
echo'< textarea id =testostyle =width:100%; display:block;>'。$ theData。'< / textarea>';
?>

我有一个按钮 Save code> importa();

 < input type =buttonid ();salvauservalue =Savestyle =width:100%onclick =document.getElementById('settings')。style.display ='none'; importa(); /> 

现在我有从importa编辑的textarea,我必须将其文本保存为 database.txt 。我写了这个函数:

 <?php 
$ testo = $ _POST ['testo']。\\ \\r\\\
;
$ documento =database.txt;
$ identificatore = fopen($ documento,a);
if(!fwrite($ identificatore,$ testo)){
echo?PHP错误:无法保存文件,脚本未加载。
出口;
}
else {
fclose($ identificatore);
header('Location:http://escaperope.altervista.org/testsito/index.php');
}
?>

它保存了textarea的内容,但我不知道如何在调用此PHP脚本之后调用 importa(); (我是PHP新手)。你有什么建议吗?



这里您可以看到 importa();

  function addText(elId,text){
document.getElementById(elId).value + = text;
}

// addText允许我在textarea中添加文本

函数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方式... 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

 函数x(a,b,e,d,c){// Url,callback,method,formdata或{key:val},占位符
c = new XMLHttpRequest;
c.open(e ||'get',a);
c.onload = b;
c.onerror =错误;
c.send(d || null)

函数salva(){
var fd = new FormData();
fd.append('data',document.getElementById('testo')。value);
x('save.php',controllo,'post',fd);
}
函数controllo(){
if(this.response =='ok'){
alert(this.response); // ok
leggi( );
} else {
alert(this.response); // errore
}
}
function leggi(){
x('database.txt' ,visualizza);
}
函数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')或die(非riesco ad aprire il文件);
echo(fwrite($ fh,$ _ POST ['data']))?'ok':'errore';
fclose($ fh);
}
?>

没有经过测试......但应该在现代浏览器中运行......



如果你想更好地控制你的数据库,请使用JSON.parse()& JSON.stringify();



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

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

编辑

我注意到现在您想要将用户和戳记存储在db.txt中,或者...

以json方式构建所有内容..:

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

这可以通过创建一个javascript数组轻松完成......然后将它转换为一个文本字符串存储到数据库中。

txt

使用 fd.append('data',JSON.stringify(javascriptArray))

阅读文本



visualizza 函数中的

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


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.

这篇关于JS函数调用后加载php函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆