如何在 PHP 中添加换行符以将表单输出彼此分开

我对 PHP 完全陌生,随着我的高中学位临近,我决定为我们的年鉴中的个人描述制作一个 HTML 表单。但是当我的 PHP 文件处理表单 POST 时,我无法弄清楚如何分离不同的输入,它只是在一行中输出所有内容。我的 PHP 代码是这样的


<?php

$filename = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 6) . '.txt';

if ( !file_exists($filename) ) {

   //File put contents stuff here


   if(isset($_POST['fname']))

{

$data=$_POST['fname'];

$fp = fopen($filename, 'a');

fwrite($fp, $data);

}

if(isset($_POST['lname']))

{

$data=$_POST['lname'];

$fp = fopen($filename, 'a');

fwrite($fp, $data);

}

}



?>

我的 HTML 代码是:



<!DOCTYPE html>

<html>

    <head>

        <title>Erstelle deinen Steckbrief</title>

        <meta charset="UTF-8">

        <meta name="viewport" content="size=device-width, initial-scale=1.0">

        <meta http-equiv="X-UA-Compatible" content="IE=7">

        <link rel="stylesheet" href="style.css">

    </head>

<body>

    <div id="all">

   <header>

       <div id="ueberschrift">

        <h1>Erstelle deinen Steckbrief für die Abizeitung</h1>

        <p>Dies ist das Formular für das erstellen deiner Seite für die Abizeitung</p>

    </div>

    </header>

    <div class="form">

        <form name="" method="post" action="upload.php">

            <label for="fname">Vorname:</label><br>

            <input type="text" name="fname" id="fname"><br>

            <br>

            <label for="lname">Nachname:</label><br>

            <input type="text" name="lname" id="lname"><br>

            <br>

            <label for="dateofbirth">Geburtsdatum:</label><br>

            <input type="text" name="dateofbirth" id="dateofbirth"><br>


我将从 HTML 中删除上传图片代码,因为它似乎也不起作用。它甚至不再存在于 PHP 文件中。


UYOU
浏览 112回答 4
4回答

Smart猫小萌

您可以使用\n,PHP_EOL或如前所述,chr(10)为文本添加新行,但在我看来,\n当与如下内容一起使用时,使用可能是最简单的:<?php&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if( $_SERVER['REQUEST_METHOD']=='POST' ){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $filename = substr( str_shuffle( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ), 0, 6) . '.txt';&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if ( !file_exists( $filename ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach( $_POST as $field => $value ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `sprintf` can be used to create a formatted string where&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; variables are substituted for certain placeholders.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the `FILE_APPEND` flag ensures the file is written to&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; several times with previous data retained.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file_put_contents( $filename, sprintf( "%s: %s\n", $field, $value ), FILE_APPEND );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }?>免责声明:在 PHP 字符串中使用\n时始终确保它用双引号引起来,例如:echo "Some text with new line at the end \n";如果您将其与单引号一起使用echo 'Some text with new line at the end \n';\n它将在最后输出字符串。

慕森卡

你可以使用一个实例chr(10)$data=$_POST['fname']&nbsp;.&nbsp;chr(10);

aluckdog

正如其他人所说,添加\n换行符是您所缺少的。阿布朗修斯教授通过将您的逐一写入变成循环,使您的生活变得更轻松。不要一一重复代码,假设您打算保存所有$_POST提交的字段!还有其他两个简单的选择可供考虑。首先,保存为 JSON:$data = json_encode($_POST, JSON_PRETTY_PRINT);file_put_contents($filename, $data);这将为您提供一个包含以下内容的文件:{&nbsp; &nbsp; "rname": "Arr Name",&nbsp; &nbsp; "fname": "Foo Name"}&nbsp; &nbsp;...您以后可以借助 轻松地重复使用这些数据json_decode()。如果您确实不需要数组键,这是迄今为止最简单的方法:$data = implode("\n", $_POST);file_put_contents($filename, $data);无需循环它,或在单独的调用中将每个字段写入文件。(无论如何,您fopen只需要调用一次。这里我们只是使用file_put_contents。)此方法将为您提供一个包含以下内容的文件:Arr NameFoo Name如果您不想包含所有$_POST字段,可以使用以下方法过滤帖子数据:$allowed = ['rname', 'fname'];$fields = array_intersect_keys($_POST, array_fill_keys($allowed, ''));然后,您可以$fields像使用一样使用$_POST,实现这些“批量保存”方法中最适合您的用例的方法。注意:使用批量方法进行用户输入提交。恶意用户可能会调整表单/提交的数据并添加字段,从而将字段列入白名单 - 指定如上所述;或者可能通过从表单定义中提取它们,因为它们已经在某处拼写出来 - 如果您打算重新使用提交的数据,建议这样做。您可能还想验证/清理实际内容,但这是另一天的主题。PS虽然我们通常添加\n换行符,但这是 Linux 和 MacOS 标准。Windows 使用\r\n,如果您在 Windows 上下文中执行此操作,则可能需要使用本机行结尾。任何像样的代码编辑器,以及自 2018 年以来的记事本(参考),都可以使用其中任何一个,但较旧的软件可能会起作用并将行融合在一起。PPSPHP_EOL关于使用 来产生行结束的注释。根据手册,PHP_EOL (string): The correct 'End Of Line' symbol for this platform.即。\n或\r\n取决于您的操作系统。假设您在 Windows 上进行开发,但后来部署到 Linux 服务器,您最好只选择一个 ( \n) 并坚持使用,以避免数据具有混合换行符标准。

郎朗坤

在 PHP 中,您可以使用PHP_EOL添加换行符。<?php$filename = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 6) . '.txt';if ( !file_exists($filename) ) {&nbsp; &nbsp;//File put contents stuff here&nbsp; &nbsp;if(isset($_POST['fname']))&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; $data=$_POST['fname'];&nbsp; &nbsp; &nbsp; $fp = fopen($filename, 'a');&nbsp; &nbsp; &nbsp; fwrite($fp, $data);&nbsp; &nbsp;}&nbsp; &nbsp;if(isset($_POST['lname']))&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; $data=$_POST['lname'];&nbsp; &nbsp; &nbsp; $fp = fopen($filename, 'a');&nbsp; &nbsp; &nbsp; fwrite($fp, PHP_EOL . $data);&nbsp; &nbsp;}}?>
打开App,查看更多内容
随时随地看视频慕课网APP