将一些表单数据传递给php控制器(OpenCart)

时间:2013-03-29 11:15:56

标签: php html

我坚持使用以下代码:

        <?php if ($orders) { ?>
        <?php foreach ($orders as $order) { ?>
        <tr>
          <td style="text-align: center;"><?php if ($order['selected']) { ?>
            <input type="checkbox" name="selected[]" value="<?php echo $order['order_id']; ?>" checked="checked" class="trigger"/>
            <?php } else { ?>
            <input type="checkbox" name="selected[]" value="<?php echo $order['order_id']; ?>" class="trigger" />
            <?php } ?></td>
          <td class="right"><?php echo $order['order_id']; ?></td>
          <td class="left"><?php echo $order['customer']; ?></td>
          <td class="left"><?php echo $order['status']; ?></td>
          <td class="right"><?php echo $order['total']; ?></td>
          <td class="left"><?php echo $order['date_added']; ?></td>
          <td class="left"><?php echo $order['date_modified']; ?></td>
          <td class="right"><?php foreach ($order['action'] as $action) { ?>
            [ <a href="<?php echo $action['href']; ?>"><?php echo $action['text']; ?></a> ]
            <?php } ?></td>
        </tr>

        <tr class="eph-row" style="" >
          <td colspan="8" style="background-color: #EBF6FF; height: 50px;">
            <div style="">
              <label for="eph-dobierka" style="display: table-row;">Dobierka</label>
                <input type="text" name="eph-dobierka" id="eph-dobierka" value="" size="7" placeholder="Dobierka"/>
            </div>
          </td>
        </tr>

        <?php } ?>
        <?php } else { ?>

order_info

这是控制器的代码:

    public function eph() {
        $this->load->model('sale/order');

        $this->data['orders'] = array();

        $orders = array();

        if (isset($this->request->post['selected'])) {
            $orders = $this->request->post['selected'];
        } elseif (isset($this->request->get['order_id'])) {
            $orders[] = $this->request->get['order_id'];
        }

$xml = "<?xml version='1.0'?>";
    $xml .= "<EPH>\n";
        $xml .= "<Zasielky>\n";

                foreach ($orders as $order_id) {
                $order_info = $this->model_sale_order->getOrder($order_id); 
                $xml .= "<Zasielka>\n";
                    $xml .= "<Adresat>\n";  
                        $xml .= "<Meno>".$order_info['shipping_firstname']." ".$order_info['shipping_lastname']."</Meno>\n";
                        $xml .= "<Organizacia>".$order_info['shipping_company']."</Organizacia>\n";
                        $xml .= "<Ulica>".$order_info['shipping_address_1']."</Ulica>\n"; 
                        $xml .= "<Mesto>".$order_info['shipping_city']."</Mesto>\n"; 
                        $xml .= "<PSC>".$order_info['shipping_postcode']."</PSC>\n"; 
                        $xml .= "<Telefon>".$order_info['telephone']."</Telefon>\n"; 
                        $xml .= "<Email>".$order_info['email']."</Email>\n"; 
                        $xml .= "<sum>".$this->request->post['eph-dobierka']."</sum>\n"; 
                    $xml .= "</Adresat>\n";   
                $xml .= "</Zasielka>\n";
                }

        $xml .= "</Zasielky>\n";            
    $xml .= "</EPH>\n";

        libxml_use_internal_errors(true);
        $xmls = simplexml_load_string($xml);

        if ($xmls === false) {
            echo "Failed loading XML\n";
            foreach(libxml_get_errors() as $error) {
                echo "\t", $error->message;
            }
        }

        Header('Content-type: text/xml');
        //Header('Content-disposition: attachment; filename="eph.xml"');
        print($xmls->asXML());




    }

如您所见,根据选定的[]值,将从数据库中查询所选行。

我将<tr class="eph-row">...</tr>部分添加到原始代码中,我想根据选择的行=&gt;将一些值传递给控制器​​。控制器中的“order foreach”中的$xml .= "<sum>".$this->request->post['eph-dobierka']."</sum>\n";这不起作用)。

1 个答案:

答案 0 :(得分:1)

此代码似乎为每个订单创建一个带name="eph-dobierka"的输入元素。这不是你想要的,因为$_POST是一个数组(内部),并且只能有一个具有特定名称的索引。 我想你想把

<label for="eph-dobierka<?php echo $order['order_id']; ?>" style="display: table-row;">Dobierka</label>
<input type="text" name="eph-dobierka[<?php echo $order['order_id']; ?>]" id="eph-dobierka<?php echo $order['order_id']; ?>" value="" size="7" placeholder="Dobierka" />

而不是

<label for="eph-dobierka" style="display: table-row;">Dobierka</label>
<input type="text" name="eph-dobierka" id="eph-dobierka" value="" size="7" placeholder="Dobierka"/>
标记中的

$xml .= "<sum>" . $this->request->post['eph-dobierka'][$order_id] . "</sum>\n";

而不是

$xml .= "<sum>" . $this->request->post['eph-dobierka'] . "</sum>\n";

在控制器中。

这假设您将在UI中为每个订单手动输入“Dobierka”的值。