2013-01-02 14 views
0

我有一個包含從數據庫中自動完成,然後當用戶提交其結果發送到PDF併發送電子郵件的形式。但是當我測試它的一行時,它的成功,但是當添加更多的行時,pdf會給我「數組」這個詞,而不是代碼。將在HTML多行時,從形式輸出給我的字「陣列」

在index.php

<?php require("login3.php"); ?> 
<?php 
if (!empty($_POST)) { 

// Used for later to determine result 
$success = $error = false; 

// Object syntax looks better and is easier to use than arrays to me 
$post = new stdClass; 

// Usually there would be much more validation and filtering, but this 
// will work for now. 
foreach ($_POST as $key => $val) 
    $post->$key = trim(strip_tags($_POST[$key])); 

// Check for blank fields 
if (empty($post->itemCode) OR empty($post->itemDesc) OR empty($post->itemQty) OR    empty($post->itemPrice) OR empty($post->itemAgent)) 
    $error = true; 

else { 

    // Get this directory, to include other files from 
    $dir = dirname(__FILE__); 

    // Get the contents of the pdf into a variable for later 
    ob_start(); 
    require_once($dir.'/pdf.php'); 
    $pdf_html = ob_get_contents(); 
    ob_end_clean(); 

    // Load the dompdf files 
    require_once($dir.'/dompdf/dompdf_config.inc.php'); 

    $dompdf = new DOMPDF(); // Create new instance of dompdf 
    $dompdf->load_html($pdf_html); // Load the html 
    $dompdf->render(); // Parse the html, convert to PDF 
    $pdf_content = $dompdf->output(); // Put contents of pdf into variable for later 

    // Get the contents of the HTML email into a variable for later 
    ob_start(); 
    require_once($dir.'/html.php'); 
    $html_message = ob_get_contents(); 
    ob_end_clean(); 

    // Load the SwiftMailer files 
    require_once($dir.'/swift/swift_required.php'); 

    $mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer 

    $message = Swift_Message::newInstance() 
        ->setSubject('New Order') // Message subject 
        ->setTo(array('[email protected]', '[email protected]' => 'Orange Pharmacy Store')) // Array of people to send to 
        ->setFrom(array('[email protected]' => 'Orange Pharmacy Order System')) // From: 
        ->setBody($html_message, 'text/html') // Attach that HTML message from earlier 
        - >attach(Swift_Attachment::newInstance($pdf_content, 'nettuts.pdf', 'application/pdf')); // Attach the generated PDF from earlier 

    // Send the email, and show user message 
    if ($mailer->send($message)) 
     $success = true; 
    else 
     $error = true; 

} 

    } 
    ?> 
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 
     <html> 
     <head> 
    <title>Orange Pharmacy Order System</title> 
    <style type="text/css" title="currentStyle"> 
      @import "css/layout-styles.css"; 
      @import "css/themes/smoothness/jquery-ui-1.8.4.custom.css"; 
</style> 

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 

<!-- jQuery libs --> 
<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script> 
<script type="text/javascript" src="js/jquery-ui-1.8.14.custom.min.js"></script> 

<!-- Our jQuery Script to make everything work --> 
<script type="text/javascript" src="js/jq-ac-script.js"></script> 

</head> 
<body> 
<h1 align="center"><img src=418694_259331167468533_385322251_n.jpg></h1> 
<div id="container"> 

    <div class="panel"> 
     <div class="title-large"> 
      <div class="theme"></div> 
     </div> 
<p> 
<h1 align="center"><b>Branch : <FONT COLOR="ORANGE"><?php echo($_SESSION["username"]); ?> <h5 align="left"><font color="black">Date : </font></b><script type="text/javascript"> 
function GetClock(){ 
d = new Date(); 
nday = d.getDay(); 
nmonth = d.getMonth(); 
ndate = d.getDate(); 
nyear = d.getYear(); 
nhour = d.getHours(); 
nmin = d.getMinutes(); 
nsec = d.getSeconds(); 

if(nyear<1000) nyear=nyear+1900; 

if(nhour == 0) {ap = " AM";nhour = 12;} 
else if(nhour <= 11) {ap = " AM";} 
else if(nhour == 12) {ap = " PM";} 
else if(nhour >= 13) {ap = " PM";nhour -= 12;} 

if(nmin <= 9) {nmin = "0" +nmin;} 
if(nsec <= 9) {nsec = "0" +nsec;} 


document.getElementById('clockbox').innerHTML=""+(nmonth+1)+"/"+ndate+"/"+nyear+"  "+nhour+":"+nmin+":"+nsec+ap+""; 
setTimeout("GetClock()", 1000); 
} 
window.onload=GetClock; 
</script> 
<?php if ($success) { ?> 
     <div class="message success"> 
      <h4>Congratulations! It worked! Now check your email.</h4> 
     </div> 
    <?php } elseif ($error) { ?> 
     <div class="message error"> 
      <h4>Sorry, an error occurred. Try again!</h4> 
     </div> 
    <?php } ?> 
    <div id="clockbox"></div></font></h5></h1> 
      <div class="content inpad"> 

        <div id="messageBox" style="margin-left:15px; padding-left:20px; padding-bottom:5px; border:1px #ccc solid; display:none;"></div> 

         <form method="post" action="#" id="itemsForm"> 

          <table id="itemsTable" class="general-table"> 
           <thead> 
           <tr> 
            <th></th> 
            <th>Item Code</th> 
            <th>Item Description</th> 
            <th>Item Qty</th> 
            <th>Item Price</th> 
            <th>Item Agent</th> 
           </tr> 
           </thead> 
           <tbody> 
            <tr class="item-row"> 
             <td></td> 
             <td><input name="itemCode" value=""  class="tInput" id="itemCode" readonly="readonly" /> </td> 
             <td><input name="itemDesc" value="" class="tInput" id="itemDesc" tabindex="1"/></td> 
             <td><input name="itemQty" value="" class="tInput" id="itemQty" tabindex="2"/></td> 
             <td><input name="itemPrice" value="" class="tInput" id="itemPrice" readonly="readonly" /> </td> 
             <td><input name="itemAgent" value="" class="tInput" id="itemAgent" readonly="readonly" /></td> 
            </tr> 
           </tbody> 
          </table> 



         <a href="#" id="addRow" class="button-clean large"><span> <img src="images/icon-plus.png" alt="Add" title="Add Row" /> Add Item</span></a> 

        </div> 
        <input type="submit" value="submit"> 
       </form> 

      </div> 
    </div> 

    </body> 
    </html> 

和輸出HTML代碼

<html> 
<head> 
<br><br> 
<h1 align="center"><img src=http://orangepharmacy.hostzi.com/418694_259331167468533_385322251_n.jpg></h1> 
<style type="text/css" title="currentStyle"> 
      @import "http://orangepharmacy.hostzi.com/css/layout-styles.css"; 
      @import "http://orangepharmacy.hostzi.com/css/themes/smoothness/jquery-ui- 1.8.4.custom.css"; 
    </style> 
    </head> 
    <body> 
    <br><br> 
    <h1 align="center"><b>Items Order <br> Branch : <FONT COLOR="ORANGE"> 
    <?php  echo($_SESSION["username"]); ?></h1> 
<br><br> 
<table id="itemsTable" class="general-table"> 
           <thead> 
           <tr> 
            <th></th> 
            <th>Item Code</th> 
            <th>Item Description</th> 
            <th>Item Qty</th> 
            <th>Item Price</th> 
            <th>Item Agent</th> 
           </tr> 
           </thead> 
           <tbody> 
            <tr class="item-row"> 
             <td></td> 
             <td><?php echo $post->itemCode; ?></td> 
             <td><?php echo $post->itemDesc; ?></td> 
             <td><?php echo $post->itemQty; ?></td> 
             <td><?php echo $post->itemPrice; ?></td> 
             <td><?php echo $post->itemAgent; ?></td> 
            </tr> 

           </tbody> 

          </table> 

<br><br><l><p><b><i><h2 align="left">Prepared by : ............................    </h2><h2 align="right"> Signeture : .................................<h2></b></i></p> 

</body> 
</html> 

的js代碼爲增加新線路:

$(document).ready(function(){ 

// Use the .autocomplete() method to compile the list based on input from user 
$('#itemDesc').autocomplete({ 
    source: 'data/item-data.php', 
    minLength: 1, 
    select: function(event, ui) { 
     var $itemrow = $(this).closest('tr'); 
       // Populate the input fields from the returned values 
       $itemrow.find('#itemCode').val(ui.item.itemCode); 
       $itemrow.find('#itemDesc').val(ui.item.itemDesc); 
       $itemrow.find('#itemPrice').val(ui.item.itemPrice); 
       $itemrow.find('#itemAgent').val(ui.item.itemAgent); 

       // Give focus to the next input field to recieve input from user 
       $('#itemQty').focus(); 

     return false; 
    } 
// Format the list menu output of the autocomplete 
}).data("autocomplete")._renderItem = function(ul, item) { 
    return $("<li></li>") 
     .data("item.autocomplete", item) 
     .append("<a>" + item.itemDesc + "</a>") 
     .appendTo(ul); 
}; 

// Get the table object to use for adding a row at the end of the table 
var $itemsTable = $('#itemsTable'); 

// Create an Array to for the table row. ** Just to make things a bit easier to read. 
var rowTemp = [ 
    '<tr class="item-row">', 
     '<td><a id="deleteRow"><img src="images/icon-minus.png" alt="Remove Item" title="Remove Item"></a></td>', 
     '<td><input name="itemCode" class="tInput" value="" id="itemCode" readonly="readonly" /> </td>', 
     '<td><input name="itemDesc" class="tInput" value="" id="itemDesc" /></td>', 
     '<td><input name="itemQty" class="tInput" value="" id="itemQty" /></td>', 
     '<td><input name="itemPrice" class="tInput" value="" id="itemPrice" readonly="readonly" /></td>', 
     '<td><input name="itemAgent" class="tInput" value="" id="itemAgent" readonly="readonly" /></td>', 
    '</tr>' 
].join(''); 

// Add row to list and allow user to use autocomplete to find items. 
$("#addRow").bind('click',function(){ 

    var $row = $(rowTemp); 

    // save reference to inputs within row 
    var $itemCode   = $row.find('#itemCode'); 
    var $itemDesc   = $row.find('#itemDesc'); 
    var $itemPrice   = $row.find('#itemPrice'); 
    var $itemQty   = $row.find('#itemQty'); 
    var $itemAgent   = $row.find('#itemAgent'); 

    if ($('#itemDesc:last').val() !== '') { 

     // apply autocomplete widget to newly created row 
     $row.find('#itemDesc').autocomplete({ 
      source: 'data/item-data.php', 
      minLength: 1, 
      select: function(event, ui) { 
       $itemCode.val(ui.item.itemCode); 
       $itemDesc.val(ui.item.itemDesc); 
       $itemPrice.val(ui.item.itemPrice); 
       $itemAgent.val(ui.item.itemAgent); 

       // Give focus to the next input field to recieve input from user 
       $itemQty.focus(); 

       return false; 
      } 
     }).data("autocomplete")._renderItem = function(ul, item) { 
      return $("<li></li>") 
       .data("item.autocomplete", item) 
       .append("<a>" + item.itemDesc + "</a>") 
       .appendTo(ul); 
     }; 
     // Add row after the first row in table 
     $('.item-row:last', $itemsTable).after($row); 
     $($itemDesc).focus(); 

    } // End if last itemCode input is empty 
    return false; 
}); 

$('#itemDesc').focus(function(){ 
    window.onbeforeunload = function(){ return "You haven't saved your Order. Are you sure you want to leave this page without saving first?"; }; 
    }); 

    }); // End DOM 

// Remove row when clicked 
$("#deleteRow").live('click',function(){ 
    $(this).parents('.item-row').remove(); 
    // Hide delete Icon if we only have one row in the list. 
    if ($(".item-row").length < 2) $("#deleteRow").hide(); 
}); 

問題是在

         <td><?php echo $post->itemCode; ?></td> 
             <td><?php echo $post->itemDesc; ?></td> 
             <td><?php echo $post->itemQty; ?></td> 
             <td><?php echo $post->itemPrice; ?></td> 
             <td><?php echo $post->itemAgent; ?></td> 

當傳遞一行時它讀取數據,但當傳遞多個行時,它返回名稱「數組」!

我嘗試這樣做,但它給我:()提供的foreach無效參數

 <td><?php foreach ($post->itemCode as $code) { 
              echo $code; 
                         } 
              ?> 
             </td> 
             <td><?php foreach ($post->itemDesc as $desc) { 
              echo $desc; 
                         } 
              ?></td> 
             <td><?php foreach ($post->itemQty as $qty) { 
              echo $qty; 
                         } 
              ?></td> 
             <td><?php foreach ($post->itemPrice as $price) { 
              echo $price; 
                         } 
              ?></td> 
             <td><?php foreach ($post->itemAgent as $agent) { 
              echo $agent; 
                         } 
              ?></td> 
+0

請您談一下增加更多的行...我看到一組在HTML領域。是否有代碼(javascript?)添加了另一組輸入,以便可以在同一個表單上輸入多個項目? –

+0

是的,有一個JS添加新的行與相同的propeties –

+3

你有點回答你自己的問題在標題。如果你有一個以上的項目,你會得到一個數組,你需要遍歷它來提取值 – Matanya

回答

2

您不能echo陣列 - 你必須遍歷並echo每個值,如

foreach ($post->itemCode as $thing) { 
    echo $thing; 
} 

如果你不知道你的陣列的結構,你可以永遠只是print_r($post->itemCode)等,而且會告訴你什麼是它。

如果要訪問數組的第一個成員,嘗試

$itemCode = $post->itemCode; echo $itemCode[0]; 
+0

我嘗試它不給我一句話陣列任何more..but它返回的最後一行僅在提交給電子郵件時才輸入?爲什麼! –