如何从html文本框中读取数据,并以与文本框相同的格式在php中打印(回显)数据?

问题描述:

I am beginner to PHP and here is my question. I have a text box and a 'Print' button. If 'Print' button is clicked, it should display the list of names entered in the text box. I'd written the following code, but the problem is, if I click the 'Print' button, it shows the names in a single line, instead of showing names in line by line format. Can someone help me in this. Thanks.

<!DOCTYPE html>
<html>
<head>
<title>Form Page</title>    
</head>
<body>

<form action="Files.php" method="get">
<textarea rows="15" cols="30" value="textbox" name="textbox"></textarea></br>
<input type="submit" value="Print" name="Print">
</form>

</body>
</html>

<?php
if(isset($_GET['Print'])){
    $file_print = $_GET["textbox"];
    echo $file_print;
}
?>

我是PHP的初学者,这是我的问题。 我有一个文本框和一个“打印”按钮。 如果单击“打印”按钮,则应显示在文本框中输入的名称列表。 我写了下面的代码,但问题是,如果我点击“打印”按钮,它会在一行中显示名称,而不是逐行显示名称。 有人可以帮助我吗? 谢谢。 p>

 &lt;!DOCTYPE html&gt; 
&lt; html&gt; 
&lt; head&gt; 
&lt; title&gt;表单页&lt; / title&gt;  
&lt; / head&gt; 
&lt; body&gt; 
 
&lt; form action =“Files.php”method =“get”&gt; 
&lt; textarea rows =“15”cols =“30”value =“textbox”  name =“textbox”&gt;&lt; / textarea&gt;&lt; / br&gt; 
&lt; input type =“submit”value =“Print”name =“Print”&gt; 
&lt; / form&gt; 
 
&lt; /  body&gt; 
&lt; / html&gt; 
 
&lt;?php 
if(isset($ _ GET ['Print'])){
 $ file_print = $ _GET [“textbox”]; 
 echo $ file_print; \  n} 
?&gt; 
  code>  pre> 
  div>

You need to use nl2br() in PHP

Replace echo $file_print; with echo nl2br($file_print);

For edit..

echo nl2br($file_print);
file_put_contents('names.txt',$file_print,FILE_APPEND); //<-- Remove FILE_APPEND if you want to overwrite the file

Use nl2br()

nl2br — Inserts HTML line breaks before all newlines in a string.

So in your case, you need to do this echo nl2br($file_print);