简体   繁体   English

使用 javascript + php 将数据写入服务器文件

[英]Writing data to a server file using javascript + php

EDIT: thanks for clearing up my confusion regardign PHP and Javascript.编辑:感谢您消除我对 PHP 和 Javascript 的困惑。 Got to use AJAX.必须使用 AJAX。

I'm just messing around trying to do a basic write to file using PHP/Javascript我只是在尝试使用PHP/Javascript对文件进行基本写入

I have a script like我有一个像

<head>
  <?php
      function writeToFile($file, $data) {
          file_put_contents($file, $data);
      }
  ?>
  <script>
      function funct() {
          <?php writeToFile('text.txt', 'hello'); ?>
      }
      window.onload = funct;
  </script>
</head>

Unfortunately, text.txt is empty.不幸的是, text.txt是空的。 What am I missing?我错过了什么?

You are confusing server side languages with client side languages.您将服务器端语言与客户端语言混淆了。

<?php
function writeToFile($file, $data) {
    file_put_contents($file, $data);
}

    writeToFile('text.txt', 'hello'); 

?>

The above will work.以上将工作。

tags are for javascript (which works on the client side - ie in the browser)标签用于 javascript(在客户端工作 - 即在浏览器中)

To call your script in PHP from a web page you need to look at AJAX - but I would start with learning a bit about client vs server side languages first - it can be confusing until it clicks!要从网页用 PHP 调用脚本,您需要查看 AJAX - 但我首先要学习一些客户端与服务器端语言的知识 - 在点击之前它可能会令人困惑!

You should use fopen to get a file handle and then use fputs to write to that file.您应该使用 fopen 获取文件句柄,然后使用 fputs 写入该文件。 On top of that, you can't just use javascript to execute php commands.最重要的是,您不能只使用 javascript 来执行 php 命令。

It works as follows:它的工作原理如下:

The user requests a .php file from the server.用户从服务器请求一个 .php 文件。 The server executes the php code in that file and writes it output as HTML in the rest of the script.服务器执行该文件中的 php 代码,并在脚本的其余部分将其输出为 HTML。 Since Javascript is executed on the clientside, the php stuff is already done by the server.由于 Javascript 是在客户端执行的,所以 php 的东西已经由服务器完成了。 So what your client actually gets is:所以你的客户实际得到的是:

<head>

  <script>
  function funct() {

  }
  window.onload = funct;
  </script>
</head>

If you want to execute some php functions while the user is doing input and don't want to reload the page, you can check ajax.如果想在用户输入的同时执行一些php函数,又不想重新加载页面,可以勾选ajax。 But for your purpose i think you should read about fopen, fputs and so on.但是为了您的目的,我认为您应该阅读有关 fopen、fputs 等的信息。

http://www.php.net/manual/de/function.fopen.php http://www.php.net/manual/de/function.fputs.php http://www.php.net/manual/de/function.fopen.php http://www.php.net/manual/de/function.fputs.php

You can't use PHP and javascript together like that.你不能像那样一起使用 PHP 和 javascript。 You should use AJAX.你应该使用 AJAX。 So rewrite the funct() like this:所以像这样重写funct()

function funct() {
  var xmlhttp = new XMLHttpRequest();

  xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          alert('Writing completed!');
      }
  }

  xmlhttp.open("GET", "script.php", true);
  xmlhttp.send();
}

Note the xmlhttp.open("GET", "script.php", true);注意xmlhttp.open("GET", "script.php", true); part.部分。 Yu must create new file to your server called script.php and add PHP contents of your script to it( writeToFile() function, function call) Yu 必须在您的服务器上创建一个名为 script.php 的新文件,并将您的脚本的 PHP 内容添加到其中( writeToFile()函数,函数调用)

Your code will not works due to the difference in execution of php and javascript .由于phpjavascript 的执行不同,您的代码将无法运行。 The php (server side) will be execute first and will execute both php(服务器端)将首先执行,并将同时执行

  <?php
     function writeToFile($file, $data) {
         file_put_contents($file, $data);
     }
  ?>

AND

   <?php writeToFile('text.txt', 'hello'); ?>

from your code and the html result will be send to the client side.从您的代码和 html 结果将被发送到客户端。 And guess what ?你猜怎么着 ? the code which will be present to javascript is the following将呈现给 javascript 的代码如下

<head>
  <script>
      function funct() {          
      }
      window.onload = funct;
  </script>
</head>

So if the file text.txt is on the client side then you need to use ajax to handle this.因此,如果文件text.txt在客户端,那么您需要使用 ajax 来处理此问题。 Here is a good introduction to Ajax这里有一个很好的 Ajax 介绍

good luck祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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