使用复选框多个选定的html表行数据未发送到电子邮件地址

时间:2018-11-21 06:19:24

标签: php

我有一个html表,用于从数据库中获取数据。我想使用复选框选择一些行数据。我已经为复选框设置了唯一值令牌否,我想将检查后的数据发送到电子邮件地址。问题是只有一行数据发送到邮件,多行数据没有发送

<form method="post" name="myForm">
<div class="col-lg-6">
    <div class="form-group" style="margin-left: 2%;">
        <div class="row">
            <input type="email" class="col-lg-9 form-control" name="email" id="email" placeholder="Enter Email Address" required="">
            <button type="submit" name="submit" class="col-lg-2 btn btn-primary"><i class="fa fa-envelope" style="margin-left: 10px;"></i> Send</button>
        </div>
    </div>
</div>
<table class="table table-bordered table-responsive" id="myTable">
    <thead>
    <tr>
        <th>Select</th>
        <th>Product</th>
        <th>Subproduct</th>
        <th>Product Name</th>
    </tr>
    </thead>
    <tbody>
    <?php
    /*select query here*/
    while($val = mysqli_fetch_assoc($retval))        
    {?>
    <tr>
        <td><input type='checkbox' name="checkbox[]" id="chk" value="<?php echo $val['tno']?>"></td>
        <td><?php echo $val['product'];?></td>
        <td><?php echo $val['subproduct'];?></td>
        <td><?php echo $val['name'];?></td>

   </tr>
   <?php           
    }
    ?>
    </tbody>
</table>

并且php代码在下面

 <?php
if(isset($_POST['submit'])){
    $checkbox = $_POST['checkbox'];
    $email = $_POST['email'];
    //$count=1;
    for($i=0;$i<count($checkbox);$i++){
        $chk_id = $checkbox[$i];
        $sql = "select * from newstock WHERE tno='$chk_id'";
        $result = mysqli_query($link,$sql);
        $val=mysqli_fetch_assoc($result);
        $product=$val['product'];
        $subproduct=$val['subproduct'];
        $name=$val['name'];
        $quantity=$val['pcs'];
        $gwt=$val['gwt'];
        $nwt=$val['nwt'];
        $tno=$val['tno'];
        $bookno=$val['bookno'];
        $dealer=$val['dealer'];
        $co='<tr>';
        $co.= '<td>Product:'. $product. '</td>';
        $co.= '<td>Subproduct: '. $subproduct . '</td>';
        $co.= '<td>Name: '. $name. '</td>';
        $co.= '<td>Pcs: '. $quantity. '</td>';
        $co.= '<td>Gross Weight: '. $gwt . '</td>';
        $co.= '<td>Net Weight: '. $nwt. '</td>';
        $co.= '<td>Token No: '. $tno. '</td>';
        $co.= '<td>Book No: '. $bookno. '</td>';
        $co.= '<td>Dealer: '. $dealer . '</td>';
        $co.= '</tr>';
    }
    $mailto = $email;
    $mailSub = "Product Details";
    require 'PHPMailer-master/PHPMailerAutoload.php';
    $mail = new PHPMailer();
    $mail ->IsSmtp();
    $mail ->SMTPDebug = 0;
    $mail ->SMTPAuth = true;
    $mail ->SMTPSecure = 'ssl';
    $mail ->Host = "smtp.gmail.com";
    $mail ->Port = 465; // or 587
    $mail ->IsHTML(true);
    $mail ->Username = "kothariorg.group@gmail.com";
    $mail ->Password = "kotharigroup@123";
    $mail ->SetFrom("noreply@kothari.com");
    $mail ->Subject = $mailSub;
    $mail ->Body = $co;
    $mail ->AddAddress($mailto);

    if(!$mail->Send())
    {
       echo "Mail Not Sent";
    }
    else
    {
        header('location: dashboard.php');
    }
}?>

2 个答案:

答案 0 :(得分:1)

检查循环。每次迭代之后

$co 

$ co变量获得新值。

这是解决方法:

$co = '';

for($i=0;$i<count($checkbox);$i++){
    $chk_id = $checkbox[$i];
    $sql = "select * from newstock WHERE tno='$chk_id'";
    $result = mysqli_query($link,$sql);
    $val=mysqli_fetch_assoc($result);
    $product=$val['product'];
    $subproduct=$val['subproduct'];
    $name=$val['name'];
    $quantity=$val['pcs'];
    $gwt=$val['gwt'];
    $nwt=$val['nwt'];
    $tno=$val['tno'];
    $bookno=$val['bookno'];
    $dealer=$val['dealer'];
    $co .='<tr>';
    $co.= '<td>Product:'. $product. '</td>';
    $co.= '<td>Subproduct: '. $subproduct . '</td>';
    $co.= '<td>Name: '. $name. '</td>';
    $co.= '<td>Pcs: '. $quantity. '</td>';
    $co.= '<td>Gross Weight: '. $gwt . '</td>';
    $co.= '<td>Net Weight: '. $nwt. '</td>';
    $co.= '<td>Token No: '. $tno. '</td>';
    $co.= '<td>Book No: '. $bookno. '</td>';
    $co.= '<td>Dealer: '. $dealer . '</td>';
    $co.= '</tr>';
}

尝试替换此代码。

答案 1 :(得分:0)

您的实际问题是此行:

$co='<tr>';

在这里,您需要在循环的每次迭代中重置$co,因此基本上可以删除到目前为止收集的所有内容。

相反,您应该考虑将在每次迭代中创建的数据放入数组中,并在完成循环后将其内爆。像这样:

[...]
$coBuffer = [];
for($i=0;$i<count($checkbox);$i++){
    [...]
    $co='<tr>';
    $co.= '<td>Product:'. $product. '</td>';
    [...]
    $co.= '</tr>';
    $coBuffer[] = $co;
}
$coBody = implode("\n", $coBuffer);

现在,您已经在最后的$coBody上收集了数据的所有“行”,可用来组装电子邮件正文。