【问题标题】:Extract data from HTML table to email via PHP通过 PHP 从 HTML 表中提取数据到电子邮件
【发布时间】:2013-08-26 21:37:02
【问题描述】:

我有一个 HTML 表格,里面有一些输入字段(只是标准的东西 - 它是一个订单),我要做的就是提取数据,以便我可以将其插入到电子邮件中。

除了从我发布到 php 电子邮件函数的表中提取数据之外,我还有其他所有工作。

我已经在谷歌上搜索了所有可能的解决方案(jQuery、javascript 等),但我还没有找到合适的解决方案。我得到的最接近的是简单地使用$(this).html(),但它不会提取输入值。

我觉得这应该是一个非常简单的解决方案,但我可以找到它。

这是我的表格示例,其中包含一些虚拟数据:

<table id="order-table">
            <tr>
                 <th>Product Name</th> 
                 <th>Quantity</th>
                 <th>X</th>
                 <th>Unit Price</th>
                 <th>=</th>
                 <th style="text-align: right; padding-right: 30px;">Totals</th> 
            </tr>
            <tr class="odd">
                <td class="product-title">Dell R720 Xeon Server</em></td>
                <td class="qty"><input type="text" class="qty-input"></input></td>
                <td class="times">X</td>
                <td class="price-per-unit">£<span>1006</span></td>
                <td class="equals">=</td>
                <td class="row-total"><input type="text" class="row-total-input" disabled="disabled"></input></td>
            </tr>
            <tr class="even">
                <td class="product-title">VMWare Standard</em></td>
                <td class="qty"><input type="text" class="qty-input"></input></td>
                <td class="times">X</td>
                <td class="price-per-unit">£<span>306</span></td>
                <td class="equals">=</td>
                <td class="row-total"><input type="text" class="row-total-input" disabled="disabled"></input></td>
            </tr>

            <tr>
                <td colspan="6" style="text-align: right;">
                    ORDER SUBTOTAL: <input type="text" class="total-box" value="£0" id="product-subtotal" disabled="disabled"></input>
                </td>
            </tr>
        </table>

任何帮助都会得到极大的欢迎。

【问题讨论】:

  • 你为什么不使用表单或者你想用 AJAX 来做呢?
  • 我不介意任何一种方式。我要做的就是创建一个简单的订单表格,提交后会通过电子邮件发送给收件人。

标签: php html extract


【解决方案1】:

如果我理解正确,您可以使用表单并命名(所有)您的输入字段。

命名您的字段名称对于本示例很重要。

命名输入字段:

<input type="text" name="qty_input_odd" class="qty-input">

然后在您的处理程序中,您将像这样使用输入变量:

$qty_input_odd=$_POST['qty_input_odd'];

其他字段也一样。

然后,您可以将所有这些信息发送到您的电子邮件,并在需要时将副本发送给您的客户。

您还可以避免使用foreach 设置输入变量。

例如:

foreach($_POST as $key => $val) {
    $message .= "$key: $val\n";
}

完整示例处理程序:

<?php

$subject = "Form submission";
$sender = preg_replace("/[\r\n,;]/", "", $_POST['email']);
$to = "email@example.com";

$headers = "From: $sender" . "\r\n" .
"Reply-To: $to" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

$redirect = "http://www.example.com/thank_you.php";
$message = "Submitted data:" . "\n\n";
foreach($_POST as $key => $val) {
    $message .= "$key: $val\n";
}

mail($to, $subject, $message, $headers);
header("Location: $redirect");
// You can use header or echo, but not both
// echo "Message sent";

?>

【讨论】:

    【解决方案2】:

    查看 jQuery 的序列化和 ajax 方法:

    http://api.jquery.com/serialize/

    http://api.jquery.com/jQuery.ajax/

    Serialize 可让您序列化页面上的所有输入,例如:

    var formParams = $('#order-table').serialize();
    

    而 ajax 方法可让您将该数据传递到服务器:

    $.ajax({
      url: url,
      data: formParams,
      success: success,
      dataType: dataType
    });
    

    【讨论】:

    • 感谢您的快速响应,从我在 jQuery 网站上看到的内容来看,这不只是给我输入吗?我需要从包括输入在内的整个表中获取数据。这可能吗?
    • 那么...您是否要同时传递输入中的值以及表格单元格内的某些值?如果是这种情况,了解为什么要从页面传递不可编辑的数据会很有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 2016-04-06
    • 2023-03-13
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多