从 HTML 文本区域读取数据,然后将数据插入到 PHP 数组中

发布于 2024-11-28 13:27:11 字数 1049 浏览 0 评论 0原文

我一直在尝试创建一个跟踪器,到目前为止一切都很顺利,只是我正在尝试开发的一项新功能让我感到困惑。我试图拥有它,以便启动跟踪器的人可以输入名称,并且跟踪器将抓取用户输入的名称并查找它们。

我能想到的实现这一目标的唯一方法是插入 $_POST['names'] 中的名称并将它们插入到一个数组中,但是当我这样做时,它只会在数组中创建一个索引。

这是我正在使用的代码:

$lol = array();

$l = array($_POST['names']);

foreach ($l as $textarea) {
    array_push($lol, nl2br($textarea));
}

for ($a = 0; $a < count($lol); $a++) {
    echo "index " . $a . ": " . $lol[$a];
}

结果是:

index 0: lol
not
sure
how
this
will
work

我在带空格的文本字段中输入了上述内容。这是我正在使用的 HTML 文件:

<body>

    <form name="form" action="test.php" method="GET">
        <div align="center">
            <textarea cols="13" rows="10" name="names"></textarea><br>
            <input type="submit" value="lol">
        </div>
    </form>

</body>

所以我想我要问的是,是否可以让人在文本框中输入“Ron”、“Mark”、“Tommi”和“Daniel”,我可以插入“Ron”、“将“Mark”、“Tommi”和“Daniel”放入一个数组中,以便它们的索引分别为 [0, 1, 2, 3],如果是这样,我将如何实现这一点?

I've been trying to create a tracker and everything is going smoothly thus far, just one new feature I'm trying to work on has me stumped. I'm trying to have it so the person who starts a tracker can enter names and the tracker will grab the names the user entered and look them up.

The only way that I can think of accomplishing this would be to insert the names from $_POST['names'] and insert them into an array, but when I do this, it only creates one index inside the array.

This is the code that I'm using:

$lol = array();

$l = array($_POST['names']);

foreach ($l as $textarea) {
    array_push($lol, nl2br($textarea));
}

for ($a = 0; $a < count($lol); $a++) {
    echo "index " . $a . ": " . $lol[$a];
}

The result of that is:

index 0: lol
not
sure
how
this
will
work

I typed the above out in the text field with spaces. Here's the HTML file that I'm using as well:

<body>

    <form name="form" action="test.php" method="GET">
        <div align="center">
            <textarea cols="13" rows="10" name="names"></textarea><br>
            <input type="submit" value="lol">
        </div>
    </form>

</body>

So I guess what I'm asking is, is it possible to have a person put, say, "Ron", "Mark", "Tommi", and "Daniel" in the text box, could I insert "Ron", "Mark", "Tommi" and "Daniel" into an array so that their index would be [0, 1, 2, 3] respectively, and if so how would I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏辞 2024-12-05 13:27:11

使用explode 将一个字符串拆分为另一个子字符串。

$lines = explode("\n", $_POST['names']);
print_r($lines);

Use explode to split a string by another substring.

$lines = explode("\n", $_POST['names']);
print_r($lines);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文