0

我正在尝试根据 tablesorter 表中所做的更改来更新 db 表。我通过 JS 发送信息,每个项目都发布到将处理更新的 php 页面。这工作正常。我现在需要让它发送一封电子邮件,其中显示买家姓名和处理的新日期,以便我们可以从中计算佣金。当我添加我的 foreach 循环时,我收到了 45 封单独的电子邮件,而不仅仅是一封。
我需要添加另一个 foreach 循环还是需要做其他事情?
这是php页面的代码:

    <?php
require_once ('../db.php');
$conn = db_connect();
session_start();


$buyer = $_POST['buyer'];
$isbn = $_POST['isbn'];
$sku = $_POST['sku'];
$cost = $_POST['cost'];
$csmt = $_POST['csmt'];
$hold = $_POST['hold'];
$today = date("Y-m-d");
$n=1;

foreach($sku as $value){ 
// update inventory table
$conn->query("update inventory set cost = $cost, csmt = $csmt, hold = $hold, date_process = $today where sku = $sku");


$holdList[$n] = array('buyer' => $buyer,
            'process date' => $today,
                );
$n++;
}
//loop trough hold list and store in csv format
$csvLists = to_csv($holdList);


function to_csv( $array ) {
 $csv = "";

 if (count($array) == 0) return "No Information found";

 ## Grab the first element to build the header
 $arr = array_pop( $array );
 $temp = array();
 foreach( $arr as $key => $data ) {
   $temp[] = $key;
 }
 $csv = implode( ',', $temp ) . "\r\n";

 ## Add the data from the first element
 $csv .= to_csv_line( $arr );

 ## Add the data for the rest
 foreach( $array as $arr ) {
   $csv .= to_csv_line( $arr );
 }

 return $csv;
}

function to_csv_line( $array ) {
 $temp = array();
 foreach( $array as $elt ) {
   $temp[] = '"' . addslashes( $elt ) . '"';
 }

 $string = implode( ',', $temp ) . "\r\n";

 return $string;
}

$conn->close();

$pricingReport = "The Latest Hold List has been completed. \n";
$pricingReport .= $csvLists;

//send email when pricing specs are done
$to = "jimd@bookcellaronline.com";

$subject = "Hold List";
$body = $pricingReport;
mail($to, $subject, $body);

?>

更新:这是JS:

    // to save the changes to the hold table
$("#holdSave").live('click', function() {
$('#holdTable tbody tr').each(function()
        {
$.ajax({
    type: "POST",
    url: "holdSave.php",
    dataType: "json",
    data: ({buyer: $(this).find('#tableBuyer').html(), sku: $(this).find('#tableSku').html(), isbn: $(this).find('#tableISBN').html(),
           cost: $(this).find('#tableCost').val(), csmt: $(this).find('#tableConsignment').val(),
           hold: $(this).find('#tableHold').val()}),
    success: function(data) {

        } // end of success function

}); // end of ajax call
                    }); // end of holdtable tbody function
}); // end of holdSave event
4

1 回答 1

2

为每条记录单独调用 ajax 函数,因此您会收到每条记录的邮件。

您可以做的是使用整个数据集创建一个 JSON 对象,然后调用 ajax 调用:

var arr={};
var cnt=0;
$('#holdTable tbody tr').each(function()
{
    arr[cnt]={buyer: $(this).find('#tableBuyer').html(), sku: $(this).find('#tableSku').html(), isbn: $(this).find('#tableISBN').html(),
       cost: $(this).find('#tableCost').val(), csmt: $(this).find('#tableConsignment').val(),
       hold: $(this).find('#tableHold').val()};
    cnt++;
}

然后,在 ajax 调用中传递这个对象:

    $.ajax({
    type: "POST",
    url: "holdSave.php",
    dataType: "json",
    data: {data:arr},
    success: function(data) {

    } // end of success function
    });

在 php 方面,您可以解析每条记录的 $_POST['data'] 。

$data=$_POST['data'];

的各个元素$data是您必须插入的记录。它们可以在一个简单的 for 循环中解析为$data[$i]['buyer'], $data[$i]['sku']...

于 2012-07-16T13:33:38.543 回答