如何通过 while 循环构建的表单通过电子邮件发送数据?

发布于 2024-10-06 01:11:45 字数 618 浏览 0 评论 0原文

作为项目的一部分,我有一个表单,我们的客户可以在其中编辑我们在 SEO 中处理的关键字列表。

这是我用来显示数据库中的关键字的代码。

<?php
$c = true;
while($row = mysql_fetch_array($result))
{
  $counter++;
  echo "<div" .(($c = !$c)?' class="right"':'') . ">";
  echo "<label for='keyword". $counter ."'>",
       "<strong>Keyword " . $counter . " </strong></label>";
  echo "<input type='text' name='keyword". $counter .
       "' id='keyword". $counter ."' value='". $row['keyword'] . "' />";
  echo "</div>";
}
?>

我不知道要做的是在将表单提交到电子邮件中时收集数据。 我已经准备好了 PHP 邮件,但在这方面有点挣扎。

有什么帮助吗?

As part of a project I have a form in which our clients can edit a list of the keywords in which we work on as part of their SEO.

This is the code I use to display the keywords we have for them on our database

<?php
$c = true;
while($row = mysql_fetch_array($result))
{
  $counter++;
  echo "<div" .(($c = !$c)?' class="right"':'') . ">";
  echo "<label for='keyword". $counter ."'>",
       "<strong>Keyword " . $counter . " </strong></label>";
  echo "<input type='text' name='keyword". $counter .
       "' id='keyword". $counter ."' value='". $row['keyword'] . "' />";
  echo "</div>";
}
?>

What I don't know what do do is collect the data when the form is submitted into an email.
I have the PHP mail bit ready but struggling on this a bit.

Any help?

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

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

发布评论

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

评论(4

2024-10-13 01:11:45

我建议将代码更改为:

<?php
$c = true;
while($row = mysql_fetch_array($result))
{
  $counter++;
  echo "<div" .(($c = !$c)?' class="right"':'') . ">";
  echo "<label for='keyword". $counter ."'>",
       "<strong>Keyword " . $counter . " </strong></label>";
  echo "<input type='text' name='keyword[]' id='keyword". $counter ."' value='". $row['keyword'] . "' />";
  echo "</div>";
}
?>

然后您可以使用 $_POST['keyword'] 访问表单目标 php 文件中的所有关键字(提交后),例如

foreach($_POST['keyword'] as $key => $value) {
      echo "Keyword #". $key." value: ". $value."<br />";
      // or your code to build your message
}

I'd recommend changing the code to this:

<?php
$c = true;
while($row = mysql_fetch_array($result))
{
  $counter++;
  echo "<div" .(($c = !$c)?' class="right"':'') . ">";
  echo "<label for='keyword". $counter ."'>",
       "<strong>Keyword " . $counter . " </strong></label>";
  echo "<input type='text' name='keyword[]' id='keyword". $counter ."' value='". $row['keyword'] . "' />";
  echo "</div>";
}
?>

You can then access all keywords in the target php file for your form (after submission) using $_POST['keyword'], eg

foreach($_POST['keyword'] as $key => $value) {
      echo "Keyword #". $key." value: ". $value."<br />";
      // or your code to build your message
}
殊姿 2024-10-13 01:11:45

不要将输入命名为“keyword1”、“keyword2”等,只需将它们全部命名为“keyword[]”。当提交表单时,PHP 会将它们全部聚合到一个数组中。

Instead of naming the inputs like "keyword1", "keyword2", etc, just name them all "keyword[]". When the form is submitted, PHP will aggregate them all into an array.

凡间太子 2024-10-13 01:11:45

要使用 POST 数据(确保您的表单使用 POST):

<?php
    $message = '<ol>';
    foreach($_POST as $key => $post) {
        $message .= "<li><strong>Keyword " . $key . ":</strong>";
        $message .= " <span>" . $post . "</span>";
        $message .= "</li>";
    }
    $message .= '</ol>';

    // mail the message here.
?>

To use the POST data (make sure your form is using POST):

<?php
    $message = '<ol>';
    foreach($_POST as $key => $post) {
        $message .= "<li><strong>Keyword " . $key . ":</strong>";
        $message .= " <span>" . $post . "</span>";
        $message .= "</li>";
    }
    $message .= '</ol>';

    // mail the message here.
?>
染柒℉ 2024-10-13 01:11:45

Brian 和 Ergo 的总结是正确的,但如果您不想修改 while 循环内的代码,您可以在最后插入一个隐藏的输入来保存最后一个 $counter 值。然后在目标 php 文件(您将在其中发送电子邮件)中,您可以加载 POST 字段(修改 Stephen 编写的内容):

<?php
$counter = (int)$_POST['counter'];
$message = '';
for($i = 1; $i <= $counter; $i++){
    $key = $_POST['keyword'.$i];
    $message .= "<p>";
    $message .= "<strong>Keyword " . $key . " </strong></label>";
    $message .= "<span> " . $post . "</span>";
    $message .= "</p>";
}

// mail message here.
?>

Brian and Ergo summary are right, but if you don't want to modify the code inside the while loop you could, at the end of that, insert an hidden input that holds the last $counter value. Then in the target php file (where you will send the email), you can load the POST fields (modifying what Stephen wrote):

<?php
$counter = (int)$_POST['counter'];
$message = '';
for($i = 1; $i <= $counter; $i++){
    $key = $_POST['keyword'.$i];
    $message .= "<p>";
    $message .= "<strong>Keyword " . $key . " </strong></label>";
    $message .= "<span> " . $post . "</span>";
    $message .= "</p>";
}

// mail message here.
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文