php - 创建文件 txt 时如何与文本保持一致 - PHP

标签 php html file text url-rewriting

我有代码

$file = fopen("file.txt",'w+');
if(isset($_POST['email']))
$string = $_POST['email'];;
fwrite($file, $string);

$string 有值:

I HAVE
NEW LINE

但是在我运行我的代码之后,我打开了 file.txt。这个看:

I HAVE NEW LINE

当我输入表单时,我希望保持相同的行。 我的表格

echo '<form method="POST" id="formmail" role="form">' .
    "\n" . '<div class="form-group">' .
        "\n" . '<input type="email" id="from" class="form-control" name="from" placeholder="From Email">' .
    "\n" . '</div>' .
    "\n" . '<div class="form-group">' .
        "\n" . '<input type="email" id="to" class="form-control" name="to" placeholder="To Email">' .
    "\n" . '</div>' .
    "\n" . '<div class="form-group">' .
        "\n" . '<input type="text" id="title" class="form-control" name="title" placeholder="Title Email">' .
    "\n" . '</div>' .
    "\n" . '<div class="form-group">' .
        "\n" . '<textarea cols="80" rows="10" id="list" placeholder="Enter List Email" class="form-control"/></textarea>' .
    "\n" . '</div>' .
    "\n" . '<div class="form-group">' .
        "\n" . '<textarea cols="80" rows="10" id="content" placeholder="Enter content Email" class="form-control"/></textarea>' .
    "\n" . '</div>' .
    "\n" . '<button id="OK" class="btn btn-danger">Sent Mail</button>' .
"\n" . '</form>';

Javascript

<script>
    $(document).ready(function(e) {
        $("#OK").click(function(e) {
            var email = $('#list').val();
            $.ajax({
                type: 'POST',
                url: 'mail.php',
                data: 'email='+email,
                success: function(answer){
                    $('.kq').html(answer);
                }
            });
            return false;
        });
    });
</script>

http://i.imgur.com/sjy0BEZ.png http://i.imgur.com/cxayF5Y.png

最佳答案

一个简单的测试来查看当您在写入文件的字符串中实际有一个换行符时会发生什么,将运行一个像这样的快速测试

$file = fopen("file.txt",'w+');

if(isset($_POST['email']))
    $string = $_POST['email'];;

$string = "I HAVE\nNEW LINE";    // this definitely has a new line
fwrite($file, $string);

如果这会用换行符写入文件,那么你的 $_POST['email']没有。

如果您的电子邮件是使用

收集的
<input type="text"

那么字符串中不会有换行符,如果它是使用

收集的
<textarea name="email"></textarea>

那么它可能有一个。但我不希望您使用 <textarea 收集电子邮件地址。

如果您使用 <input type="email"那么请记住

email: The element represents one email address. Line breaks are automatically stripped from the input value. An invalid email address can be set, but the input field will only satisfy its constraints if the email address satisfies the ABNF production 1*( atext / "." ) "@" ldh-str 1*( "." ldh-str ) where atext is defined in RFC 5322 section 3.2.3, and ldh-str is defined in RFC 1034 section 3.5.

实际上现在我看到你的 HTML 有一个更明显的问题!!

您为包含电子邮件地址的字段使用了错误的名称,它应该是 $_POST['from']$_POST['to']

或者如果您希望从

<textarea cols="80" rows="10" id="list" placeholder="Enter List Email" class="form-control"/></textarea>'

那么你实际上并没有给它一个 name="email"属性,因此不会向 $_POST 返回任何内容,除非您使用 javascript 发送数据,在这种情况下显示 JAVASCRIPT 代码。

关于php - 创建文件 txt 时如何与文本保持一致 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31240419/

相关文章:

php - 使用 Ext.Panel 如何将 .php 页面指定为 html 源?

php - mysql 文本搜索 OR 语法

JavaScript Insert table into text editor without plugins(不建议使用插件)

java - 在Java中使用MappedByteBuffer读取文件的每一行

C - _IOLBF 自定义缓冲区不加载单行

php - 在 PHP 中声明一个类的 ctor 'final' 是不好的做法吗?

javascript - 如何让 HTML 标签在 Angular.js 中工作?

html - 悬停对 class 和 id 的影响是否相互关联?

写入文件时 Android 权限被拒绝

javascript - 在 PHP 中获取 MySQL DateTime 到 JavaScript 中的秒