简体   繁体   English

如何将项目从 HTML 导出到 .TXT 文件?

[英]How can I export items from HTML into a .TXT file?

I am trying to create a HTML program that can put the selected item from the dropdown created from the <select> and <option> scripts into a.txt file.我正在尝试创建一个 HTML 程序,该程序可以将<select><option>脚本创建的下拉列表中的所选项目放入 a.txt 文件中。 The file is on Replit, so assume the text file already exists.该文件位于 Replit 上,因此假设该文本文件已经存在。 This is my code, so far:到目前为止,这是我的代码:

` `

<!DOCTYPE html>
<html>
  <body>
    <form action="cardspicked.txt" method="get">
      <h3>What is your favorite low elixir Clash Royale card?</h3>
      <label for="low_elixir">Choose a card:</label>
      <select name = "low_elixir" id = "low_elixir">
        <option>Heal Spirit</option>
        <option>Skeletons</option>
        <option>Electro Spirit</option>
        <option>Fire Spirit</option>
        <option>Ice Spirit</option>
        <option>Wall Breakers</option>
        <option>Bats</option>
        <option>Spear Goblins</option>
        <option>Bomber</option>
        <option>Ice Golem</option>
        <option>Goblins</option>
        <option>Rage</option>
        <option>Giant Snowball</option>
        <option>Barbarian Barrel</option>
        <option>Zap</option>
        <option>The Log</option>
      </select>
      <hr>
      <h3>What about medium elixir?</h3>
      <label for="med_elixir">Choose a card:</label>
      <select name = "med_elixir" id = "med_elixir">
        <option>Knight</option>
        <option>Ice Wizard</option>
        <option>Mega Minion</option>
        <option>Dart Goblin</option>
        <option>Goblin Gang</option>
        <option>Miner</option>
        <option>Minions</option>
        <option>Bandit</option>
        <option>Princess</option>
        <option>Guards</option>
        <option>Archers</option>
        <option>Firecracker</option>
        <option>Royal Ghost</option>
        <option>Elixir Golem</option>
        <option>Skeleton Barrel</option>
        <option>Fisherman</option>
        <option>Skeleton Army</option>
        <option>Battle Healer</option>
        <option>Zappies</option>
        <option>Skeleton King</option>
        <option>Hunter</option>
        <option>Valkyrie</option>
        <option>Flying Machine</option>
        <option>Mighty Miner</option>
        <option>Electro Wizard</option>
        <option>Magic Archer</option>
        <option>Night Witch</option>
        <option>Inferno Dragon</option>
        <option>Battle Ram</option>
        <option>Mini P.E.K.K.A</option>
        <option>Musketeer</option>
        <option>Baby Dragon</option>
        <option>Golden Knight</option>
        <option>Skeleton Dragons</option>
        <option>Dark Prince</option>
        <option>Night Witch</option>
        <option>Lumberjack</option>
        <option>Cannon</option>
        <option>Tombstone</option>
        <option>Mortar</option>
        <option>Bomb Tower</option>
        <option>Tesla</option>
        <option>Furnace</option>
        <option>Goblin Cage</option>
        <option>Goblin Drill</option>
        <option>Goblin Barrel</option>
        <option>Royal Delivery</option>
        <option>Tornado</option>                     
        <option>Earthquake</option>
        <option>Arrows</option>
        <option>Clone</option>
        <option>Fireball</option>
        <option>Freeze</option>
        <option>Poison</option>
      </select>
      <hr>
      <h3>High elixir?</h3>
      <label for="high_elixir">Choose a card:</label>
      <select name = "high_elixir" id = "high_elixir">
        <option>Barbarians</option>
        <option>Royal Hogs</option>
        <option>Giant</option>
        <option>Prince</option>
        <option>Wizard</option>
        <option>Ram Rider</option>
        <option>Cannon Cart</option>
        <option>Rascals</option>
        <option>Witch</option>
        <option>Minion Horde</option>
        <option>Executioner</option>
        <option>Balloon</option>
        <option>Archer Queen</option>
        <option>Bowler</option>
        <option>Electro Dragon</option>
        <option>Elite Barbarians</option>
        <option>Goblin Giant</option>
        <option>Sparky</option>
        <option>Royal Giant</option>
        <option>Giant Skeleton</option>
        <option>Mega Knight</option>
        <option>P.E.K.K.A</option>
        <option>Royal Recruits</option>
        <option>Lava Hound</option>
        <option>Electro Giant</option>
        <option>Golem</option>
        <option>Three Musketeers</option>
        <option>Goblin Hut</option>
        <option>Inferno Tower</option>
        <option>Elixir Collector</option>
        <option>X-Bow</option>
        <option>Barbarian Hut</option>
        <option>Graveyard</option>
        <option>Lightning</option>
        <option>Rocket</option>
      </select>
      <hr>
        <button type="submit" value="Submit" />Submit</button>

     </form>
  </body>
</html>

` `

I have checked many websites, including this one, to find an answer to this question, but none have been able to help me.我已经检查了很多网站,包括这个网站,以找到这个问题的答案,但没有一个能够帮助我。 Try to help, if possible.如果可能,请尝试提供帮助。

  1. HTML is not a programming language; HTML 不是编程语言; it is a markup language.它是一种标记语言。 You cannot use algorithms or anything else you would use in a traditional programming language in HTML.您不能在 HTML 中使用算法或任何您会在传统编程语言中使用的其他内容。
  2. JavaScript is client-side only. JavaScript 只是客户端。 It is impossible to store anything on the server side from the client side using pure JavaScript;使用纯 JavaScript 不可能从客户端在服务器端存储任何东西; you need PHP for this.为此,您需要 PHP。 The only exception is if you have a backend running Node.js.唯一的例外是如果你有一个运行 Node.js 的后端。

Check out this article for more info on server- vs. client-side programming.查看本文以获取有关服务器端与客户端编程的更多信息。

In order to add such functionality using PHP, you would need to send a PHP POST request to the file on the server you want to add to.为了使用 PHP 添加此类功能,您需要向要添加到的服务器上的文件发送 PHP POST 请求。 To learn more about PHP and form control using PHP, I suggest this article .要了解有关 PHP 和使用 PHP 的表单控件的更多信息,我建议阅读这篇文章

You can not do this on HTML alone as HTML is not a programming language.您不能单独在 HTML 上执行此操作,因为 HTML 不是一种编程语言。 You will need to use some JS which can be achieved like this:您将需要使用一些可以像这样实现的 JS:

 const fs = require('fs'); var data = document.getElementById("low_elixir").options[].text; fs.writeFile('data.txt', data, (err) => { if (err) throw err; })
 <select name="low_elixir" id="low_elixir"> <option>Heal Spirit</option> <option>Skeletons</option> <option>Electro Spirit</option> <option>Fire Spirit</option> <option>Ice Spirit</option> <option>Wall Breakers</option> <option>Bats</option> <option>Spear Goblins</option> <option>Bomber</option> <option>Ice Golem</option> <option>Goblins</option> <option>Rage</option> <option>Giant Snowball</option> <option>Barbarian Barrel</option> <option>Zap</option> <option>The Log</option> </select>

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

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