通过PHP将HTML表中的数据提取到电子邮件中


Extract data from HTML table to email via PHP

我有一个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>

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

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

在本例中,命名字段名称非常重要

命名输入字段:

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

然后在处理程序中,您将使用inputs的变量,如下所示:

$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";
?>

查看jQuery的serialize和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
});