2016-09-29 63 views
0

對不起,如果這是一個不好的描述,我相對比較新。如何使用php中的按鈕通過電子郵件發送包含mysql結果數據的html表格?

我正在開發一個web工具,它顯示由html表格中日期範圍過濾的mysql數據的結果。我有一個輸入和按鈕,允許用戶輸入一個電子郵件地址發送表數據。問題是我無法正確傳遞html表格數據到我的PHP頁面通過電子郵件發送。

我已經嘗試過使用AJAX和Javascript的各種方法,但沒有任何工作。有一次,我能夠傳遞表格數據,但沒有輸入電子郵件(反之亦然);從來沒有同時。我需要能夠傳遞已輸入的電子郵件以及html表格中的數據,最好還要保留表格格式以便更清晰地查看)。任何幫助和/或洞察力非常感謝。道歉,如果這個代碼是不正確的和/或錯誤的。

相關PHP:

<!-- download2csv button --> 

    <div> 
    <form action="getCSV.php" method="post"> 
     <input type="hidden" name="csv_text" id="csv_text_computers"> 
     <button class="btn btn-app btn-primary btn-xs no-radius" type="submit" onclick="getCSVDataComputers()"> 
     <i class="icon-save"></i></button> 
    </form> 

<!-- email button --> 

    <form action="mailto.php" method="post"> 
     Email: <input type="text" name="einame" id="eiid" placeholder="[email protected]"> 
     <button class="btn btn-app btn-info btn-xs no-radius" type="submit" name="ebname" id="ebid" onclick="mailer()"> 
     <i class="icon-envelope"></i></button> 
    </form> 
    </div> 

</div> 

<!-- data table header --> 

<div class="table-responsive"> 
    <table id="table-computers1" name="table-computers" class="table table-striped table-bordered table-hover"> 

     <thead> 
      <tr> 
       <th>Item Name</th> 
       <th>RFID Number</th> 
       <th>Link</th> 
       <th>Delivery Date</th> 
      </tr> 
     </thead> 

     <tbody> 
      <?php 
      foreach ($infocoms as $infocom) 
      { 
       //variables for glpi url 
       $itemtype = $infocom['itemtype']; 
       $items_id = $infocom['items_id']; 
       $url = "https://null.null.com/front/" . $itemtype. ".form.php?id=" . $items_id; 
      ?> 
      <tr> 
       <?php 
       // database connection 
       $conn = mysqli_connect('null', 'null', 'null'); 
       mysqli_select_db($conn, 'glpi'); 

       if ($infocom['itemtype'] == "peripheral") { 
        $query = "SELECT glpi_peripherals.name AS devicename, TRIM(LEADING '0' FROM glpi_peripherals.otherserial) AS otherserial FROM glpi_peripherals INNER JOIN glpi_infocoms ON glpi_peripherals.id = glpi_infocoms.items_id WHERE glpi_infocoms.items_id = $infocom[items_id] AND glpi_infocoms.itemtype = 'peripheral'"; 
        $result = mysqli_query($conn,$query) or die(mysqli_error($conn)); 
        $devices = $result->fetch_assoc(); 
        echo "<td>$devices[devicename]</td>"; 
        echo "<td><center>$devices[otherserial]</center></td>"; 

       } elseif ($infocom['itemtype'] == "computer") { 
        $query = "SELECT glpi_computers.name AS compname, TRIM(LEADING '0' FROM glpi_computers.otherserial) AS otherserial FROM glpi_computers INNER JOIN glpi_infocoms ON glpi_computers.id = glpi_infocoms.items_id WHERE glpi_infocoms.items_id = $infocom[items_id] AND glpi_infocoms.itemtype = 'computer'"; 
        $result = mysqli_query($conn,$query) or die(mysqli_error($conn)); 
        $computers = $result->fetch_assoc(); 
        echo "<td>$computers[compname]</td>"; 
        echo "<td><center>$computers[otherserial]</center></td>"; 

       } elseif ($infocom['itemtype'] == "monitor") { 
        $query = "SELECT glpi_monitors.name AS viewname, TRIM(LEADING '0' FROM glpi_monitors.otherserial) AS otherserial FROM glpi_monitors INNER JOIN glpi_infocoms ON glpi_monitors.id = glpi_infocoms.items_id WHERE glpi_infocoms.items_id = $infocom[items_id] AND glpi_infocoms.itemtype = 'monitor'"; 
        $result = mysqli_query($conn,$query) or die(mysqli_error($conn)); 
        $monitors = $result->fetch_assoc(); 
        echo "<td>$monitors[viewname]</td>"; 
        echo "<td><center>$monitors[otherserial]</center></td>"; 

       } elseif ($infocom['itemtype'] == "networkequipment") { 
        $query = "SELECT glpi_networkequipments.name AS netname, TRIM(LEADING '0' FROM glpi_networkequipments.otherserial) AS otherserial FROM glpi_networkequipments INNER JOIN glpi_infocoms ON glpi_networkequipments.id = glpi_infocoms.items_id WHERE glpi_infocoms.items_id = $infocom[items_id] AND glpi_infocoms.itemtype = 'networkequipment'"; 
        $result = mysqli_query($conn,$query) or die(mysqli_error($conn)); 
        $networks = $result->fetch_assoc(); 
        echo "<td>$networks[netname]</td>"; 
        echo "<td><center>$networks[otherserial]</center></td>"; 

       } elseif ($infocom['itemtype'] == "printer") { 
        $query = "SELECT glpi_printers.name AS printname, TRIM(LEADING '0' FROM glpi_printers.otherserial) AS otherserial FROM glpi_printers INNER JOIN glpi_infocoms ON glpi_printers.id = glpi_infocoms.items_id WHERE glpi_infocoms.items_id = $infocom[items_id] AND glpi_infocoms.itemtype = 'networkequipment'"; 
        $result = mysqli_query($conn,$query) or die(mysqli_error($conn)); 
        $printers = $result->fetch_assoc(); 
        echo "<td>$printers[printname]</td>"; 
        echo "<td><center>$printers[otherserial]</center></td>"; 

       } else { 
        // error handling for null entries 
        echo "1 or more items were not found"; 
       } 
       // end data loop and close mysql connection 
       mysqli_close($conn); 
       ?> 
       <td><a target=_blank href=$url><?php echo $url; ?></a></td> 
       <td><center><?php echo $infocom['ddate']; ?></center></td> 
      </tr> 
      <?php 
      } 
      ?> 
     </tbody> 
    </table> 
</div> 
</form> 
</div> 
<?php 
} else { 
    echo "<center><p style='font-size:125%;'>Please select a date range to submit.</p></center>"; 
} 
error_log("\n"); 
?> 

相關的Javascript:

<script type="text/javascript"> 
jQuery(function($) { 
    var oTable1 = $('#table-computers').dataTable({ 
    "aLengthMenu": [[10, 25, 100, -1], [10, 25, 100, "All"]], 
    "iDisplayLength": -1, 
    "aoColumns": [ 
     { "bSortable": true }, 
     null, null,null, 
    ] }); 
}) 

function getCSVDataComputers(){ 
var csv_value=$('#table-computers').table2CSV({delivery:'value'}); 
$("#csv_text_computers").val(csv_value); 
} 

</script> 

<script type="text/javascript"> 

function mailer()  
{  
    var tableContent=document.getElementById("table-computers").innerHTML; 
    alert(tableContent); // This works, but data does not echo in mailto.php 
    $.post('mailto.php',{content:tableContent},function(data) { 
    }); 
} 

</script> 

郵寄地址PHP頁面:

<?php 
$mailto = $_POST['einame']; 
$table = $_POST['tableContent']; 

$headers .= 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'From: Test <[email protected]>' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

if (isset($mailto)) { 
mail($mailto,"GLPI Notifications",$table,$headers); 
echo "Mail sent successfully to $mailto." . "\r\n"; 
} else { 
echo "Mail was not sent." . "\r\n"; 
} 

?> 

<br> 
<a href="blank.php">Home Page</a> 
<br> 
<?php 
echo $table; // Displays nothing 
error_log("\n"); 
?> 
+0

在這部分,你不能做類似於:$ .post('mailto.php',{「content」:tableContent,「email」:$('input [name = einame]')。val()},function(data)...'?不會發送'$ _POST ['content']'和'$ _POST ['email']'?看起來好像可能。 – Rasclatt

回答

0

終於想通了。更新代碼:

function mailer() {  
    var tableContent=document.getElementById("table-computers").innerHTML; 
    // alert(tableContent); confirmation 
    $.post("mailto.php",{ "content": $("#bmailto").val(tableContent) },function(data) { 
    }); 
} 
相關問題