如何通过 while 循环构建的表单通过电子邮件发送数据?
作为项目的一部分,我有一个表单,我们的客户可以在其中编辑我们在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议将代码更改为:
然后您可以使用
$_POST['keyword']
访问表单目标 php 文件中的所有关键字(提交后),例如I'd recommend changing the code to this:
You can then access all keywords in the target php file for your form (after submission) using
$_POST['keyword']
, eg不要将输入命名为“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.
要使用 POST 数据(确保您的表单使用 POST):
To use the POST data (make sure your form is using POST):
Brian 和 Ergo 的总结是正确的,但如果您不想修改 while 循环内的代码,您可以在最后插入一个隐藏的输入来保存最后一个 $counter 值。然后在目标 php 文件(您将在其中发送电子邮件)中,您可以加载 POST 字段(修改 Stephen 编写的内容):
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):