如何将以HTML格式输入的字符串保存为文本文件 [英] How to save string entered in HTML form to text file

查看:658
本文介绍了如何将以HTML格式输入的字符串保存为文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的PHP文件。我想有两个用于用户输入的文本框和一个提交按钮。用户将输入他们的名字和姓氏,然后我想添加或创建一个TXT文件,输入从field1和field2输入的数据。



可能我正在进行这是错误的方式。

 < html>我将会发布两种我一直在琢磨的方式。 
< head>
< title> Field1& 2'; /标题>
< / head>
< body>
< form>
你叫什么名字?< br>
< input type =textname =field1>< br>
< input type =textname =field2>< br>
< input type =submitvalue =提交>
< / form>

<?php
$ txt = $ _POST ['field1']。' - '。$ _ POST ['field2'];
$ var_str3 = var_export($ txt,true); //是否有必要?
$ var3 =$ var_str3; //是否有必要?
file_put_contents('fields.txt',$ var3.PHP_EOL,FILE_APPEND);
?>

< / body>
< / html>

我无法弄清楚如何将field1和field2中的数据转换为字符串变量。



我也使用这个php而不是上面列出的部分

 <?php 
$ txt =data.txt;
if(isset($ _ POST ['field1'])& isset($ _ POST ['field2'])){
$ fh = fopen($ txt,'a');
$ txt = $ _ POST ['field1']。' - '。$ _ POST ['field2'];
fwrite($ fh,$ txt); //将信息写入文件
fclose($ fh); //关闭文件
}
?>


解决方案

您应该了解 HTML表单和PHP 表单处理



在您的代码中,您必须使用表单HTTP方法。并且表单数据必须发送到PHP文件进行处理。

在这段代码中,我使用HTTP PSOT方法,您也可以使用GET方法,结果将相同。这两种方法用于收集表单数据。而php文件名是action.php

index.html

 < html> 
< head>
< title>字段1& 2'; /标题>
< / head>
< body>
< form action =action.phpmethod =post>
你叫什么名字?< br>
< input type =textname =field1>< br>
< input type =textname =field2>< br>
< input type =submitvalue =提交>
< / form>
< / body>
< / html>

action.php

 <?php 
$ path ='data.txt';
if(isset($ _ POST ['field1'])& isset($ _ POST ['field2'])){
$ fh = fopen($ path,a +);
$ string = $ _POST ['field1']。' - '。$ _ POST ['field2'];
fwrite($ fh,$ string); //将信息写入文件
fclose($ fh); //关闭文件
}
?>


I have a very basic PHP file. i want to have two textboxes for user input, and a submit button. The user will enter their first and last name, then i would like to append or create a TXT file with the data entered from field1 and field2.

Possibly i am going about this the wrong way. I will post two of the ways i have been tinkering around with.

<html>
 <head>
  <title>Field1 & 2</title>
 </head>
 <body>
  <form>
  What is your name?<br>
  <input type="text" name="field1"><br>
    <input type="text" name="field2"><br>
   <input type="submit" value="Submit">
  </form>

 <?php
$txt= $_POST['field1'].' - '.$_POST['field2']; 
$var_str3 = var_export($txt, true);        //is this necessary?
$var3 = "$var_str3";                       //is this necessary? 
file_put_contents('fields.txt', $var3.PHP_EOL, FILE_APPEND);
?>

 </body>
    </html>

I cant figure out how to get the data from field1 and field2 into a string variable.

I have also messed around with using this php instead of the section listed above

  <?php
 $txt= "data.txt";
 if (isset($_POST['field1']) && isset($_POST['field2'])) {
$fh = fopen($txt, 'a'); 
    $txt=$_POST['field1'].' - '.$_POST['field2']; 
   fwrite($fh,$txt); // Write information to the file
   fclose($fh); // Close the file
 }
?>

解决方案

You should learn about HTML Forms And PHP Form Handling.

In your code you have to use a form HTTP method. And the form data must sent for processing to a PHP file.

In this code i use HTTP PSOT method you can also use GET method the result will be same. This two method is used for collecting the form data. And the php file name is "action.php".

index.html

  <html>
     <head>
      <title>Field 1 & 2</title>
     </head>
     <body>
        <form action="action.php" method="post">
          What is your name?<br>
          <input type="text" name="field1"><br>
            <input type="text" name="field2"><br>
           <input type="submit" value="Submit">
        </form>
     </body>
    </html>

action.php

<?php
 $path = 'data.txt';
 if (isset($_POST['field1']) && isset($_POST['field2'])) {
    $fh = fopen($path,"a+");
    $string = $_POST['field1'].' - '.$_POST['field2'];
    fwrite($fh,$string); // Write information to the file
    fclose($fh); // Close the file
 }
?>

这篇关于如何将以HTML格式输入的字符串保存为文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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