简体   繁体   English

如何使用JQuery / JavaScript / PHP将文本写入外部文件?

[英]How can I write text to an external file using JQuery/JavaScript/PHP?

I have some text stored in a POST variable that I need to append to an HTML file. 我在POST变量中存储了一些文本,需要将它们附加到HTML文件中。 I want to do within my PHP webpage, but it could use HTML, JQuery, Javascript, or anything else that will run natively. 我想在我的PHP网页中进行操作,但是它可以使用HTML,JQuery,Javascript或将在本机运行的任何其他方式。 I've tried the PHP approach, but it hasn't worked despite my numerous attempts. 我已经尝试过PHP方法,但是尽管我进行了无数次尝试,但该方法仍然无效。 Always a 500 internal server error. 始终是500个内部服务器错误。 And I can't find anything on writing to files using JQuery, so if anyone out there knows something I don't, it would be appreciated. 而且我在使用JQuery写入文件时找不到任何东西,因此,如果那里有人知道我不知道的内容,将不胜感激。 Thanks :) 谢谢 :)

Writing directly to you html/php documents is extremely bad practice. 直接向您编写html / php文档是非常糟糕的做法。 As your comment said you need to validate the input and if you make a mistake you could screw up your whole website. 正如您的评论所说,您需要验证输入内容,如果输入有误,则可能会破坏整个网站。

There are a couple of options you could use: 您可以使用几个选项:

  • localstorage: This saves the changes to the browser of the user. localstorage:这会将更改保存到用户的浏览器中。 This means that the changes wont be on the server. 这意味着更改不会在服务器上。 Very easy solution. 非常简单的解决方案。
  • Add a database: With PHP it is very easy to add a SQL databse. 添加数据库:使用PHP,添加SQL数据库非常容易。 Write some functions that write the data to the database and read the data from there. 编写一些函数,将数据写入数据库并从中读取数据。 Safer than writing directly to HTML/PHP but still needs validation 比直接编写HTML / PHP更安全,但仍需要验证
  • Do some extreme reading into all the PHP IO functions. 对所有PHP IO功能进行一些极端的阅读。 You need some extreme good handling if you want this site live. 如果要使该站点正常运行,则需要一些非常好的处理。

I think that with the DB you`ll manage, just be safe and build in some security 我认为使用DB,您将可以安全,安全地建立管理

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

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