0

我有一个表单,其中包含来自数据库的自动完成,然后当用户提交时,它将结果发送到 pdf 并将其发送到电子邮件.. 但是当我测试它的一行时它成功但是当添加更多行时,pdf 给我这个词“数组”是代码。

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('hussammoz@aol.com',  'hussammoz@gmail.com' => 'Orange Pharmacy Store')) // Array of people to send to
                   ->setFrom(array('no-reply@net.tutsplus.com' => '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>
4

1 回答 1

3

你不能echo一个数组 - 你必须迭代它和echo每个值,例如

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

如果你不确定你的数组的结构,你总是可以print_r($post->itemCode)等等,这会告诉你里面有什么。

如果要访问数组的第一个成员,请尝试

$itemCode = $post->itemCode; echo $itemCode[0];
于 2013-01-02T18:24:19.630 回答