【问题标题】:Send dynamic array in form via $_POST to html email通过 $_POST 将动态数组以表单形式发送到 html 电子邮件
【发布时间】:2013-06-14 19:18:18
【问题描述】:

这个标题可能不能很好地描述我的问题,但我不确定如何命名这篇文章......无论如何,我有一个动态生成输入框的表单,它使用以下内容提取过去 4 年:

<?php
    $current_date = new DateTime();
    for ($i = 1; $i <= 4; $i++) {
        $current_date->modify('-1 year');
        $date_string = $current_date->format('Y')
?>

<fieldset name="gross_sales">
    <input type="number" name="year_brand_gross[<?php echo $date_string; ?>]" placeholder="Gross Sales for <?php echo $date_string; ?>">
</fieldset>

<?php
} // end while
?>

一旦用户点击提交,表单数据就会通过我的 process.php 文件进行处理,该文件包含以下内容:

$year_brand_gross[1] = $_POST['year_brand_gross'][1];
$year_brand_gross[2] = $_POST['year_brand_gross'][2];
$year_brand_gross[3] = $_POST['year_brand_gross'][3];
$year_brand_gross[4] = $_POST['year_brand_gross'][4];

现在我很确定上面的部分是不正确的。所以这是我的问题......我如何从这些输入中获取信息到我的电子邮件中,因为它们是由数组创建的,而不是“实际上”在那里。这是我发送的 html 电子邮件的精简版本,我很确定这也是错误的,因为上面的代码不正确:

<table>
    <tr>
        <td>Gross Sales:</td>
    </tr>
    <tr>
        <td>{$year_brand_gross[1]}</td>
        <td>{$year_brand_gross[2]}</td>
        <td>{$year_brand_gross[3]}</td>
        <td>{$year_brand_gross[4]}</td>
    </tr>
</table>

非常感谢任何帮助!

【问题讨论】:

  • 我们能看到整个代码吗?这可能会有所帮助。
  • $_POST['year_brand_gross'][1] 应改为$_POST['year_brand_gross'][2012]
  • 那么代码需要每年更新。我们这样做是为了使代码不需要更新。 @tntu 整个代码大约有 3,000 行,并且是半机密的。我已经发布了所有必要的代码。

标签: php forms post html-email


【解决方案1】:

您的表单实际上看起来像

<input type="number" name="year_brand_gross[2012]" ... />
<input type="number" name="year_brand_gross[2011]" ... />
<input type="number" name="year_brand_gross[2010]" ... />
etc...

这意味着你需要使用

$_POST['year_brand_gross'][2012]
$_POST['year_brand_gross'][2011]
$_POST['year_brand_gross'][2010]
etc...

在服务器上。

【讨论】:

  • 嗨,马克,感谢您的意见!但恐怕这行不通。我们使用了一个在过去 4 年中动态输入的数组,这样我们就不必每年都更新代码。所以今年它将显示 2013-2009,而明年它将显示 2014-2010...
  • 我了解用户查看 php 代码时“打印”的内容。问题是因为我们使用的是动态生成的输入,所以我不确定下面的 # 符号在哪里,因为它实际上不可能是一年:$year_brand_gross# = $_POST['year_brand-gross'][ #]
  • foreach($_POST['year_brand_gross'] as $year =&gt; $value) 也是如此。您将获得嵌入的年份及其相关值,而无需关心它们是什么年份
  • 好的,我可以替换以下内容:$year_brand_gross[1] = $_POST['year_brand_gross'][1]; $year_brand_gross[2] = $_POST['year_brand_gross'][2]; $year_brand_gross[3] = $_POST['year_brand_gross'][3]; $year_brand_gross[4] = $_POST['year_brand_gross'][4];你在上面发布了什么?
【解决方案2】:
foreach($_POST['year_brand_gross'] AS $yeah => $value) {
  // use $year and $value variables to do whatever
  // this code will execute once for each values in $_POST['year_brand_gross'].
  // note: print_r($_POST);
  // $_POST is an array, same for $_GET and so on
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 2013-01-20
    • 2014-02-22
    • 2022-10-23
    相关资源
    最近更新 更多