1

我是php新手,所以这里...

我正在尝试使用函数将从我的 SESSION 变量中排序的所有项目输出到输入值中,以便我可以通过表单提交来处理它们。现在输入字段已成功创建并且一切正常。这是代码

function list_cart_items() {
    $num =0;
    $total=0;
    foreach($_SESSION as $name => $value) {
        if ($value!=0) {
            if (substr($name, 0, 5)=='cart_') {
            $id = substr($name, 5, (strlen($name)-5));
            $iget = mysql_query('SELECT id,name,price FROM items WHERE id='.mysql_real_escape_string((int)$id));
            while ($iget_row = mysql_fetch_array($iget)) {
            $num ++;
            $total += number_format(($iget_row['price']*$value),2);
            echo "<tr>
            <td><input type='text' name='order_item".$num."' value='".$num."' size='2' maxlength='2'>
            <td><input type='text' name='order_qty_item".$num."' value='".number_format($value,1)."' size='3' maxlength='3'>
            <td><input type='text' name='order_name_item".$num."' value='".$iget_row['name']."' size='30' maxlength='30'>
            <td><input type='text' name='order_value_item".$num."' value='R ".number_format(($iget_row['price']*$value), 2)."' size='6' maxlength='6'>
            </tr>";
        }   
        }
    }
}
    echo "<tr><td colspan='2'><b>Order Total</b></td><td></td><td colspan='2'><b>R ".$total."</b></td></tr>";

这是使用提交按钮创建实际表单的代码

    echo "<div id='shoppingcart'><form action='' method='POST'>";
    echo "<table class='checkout'><caption><b>Shopping Cart</b></caption>";
    **list_cart_items();**
    echo "<div id='information'>";
    echo "<tr><th colspan='2'>Name and Surname:</th><td colspan='3'><input type='text' size='45' maxlength='50' name='Contact' id='Contact'></td><tr>";
    echo "<tr><th colspan='2'>Telephone number:</th><td colspan='3'><input type='text' size='15' maxlength='12' name='Telephone' id='Telephone'></td><tr>";
    echo "<tr><th colspan='2'>Delivery Address:</th><td colspan='3'><textarea rows='8' cols='45' id='address' name='address'>Enter Address Here:</textarea></td><tr>";
    echo "<tr><th colspan='2'>Instructions:</th><td colspan='3'><textarea rows='8' cols='45' id='instructions' name='instructions'></textarea></td><tr>";
    echo "<tr><td colspan='5' align='center'><button type='submit' name='submit' value='submit' style='border: 0; background: transparent'>
   <img src='images/buynow.jpg' class='button' alt='submit'/>
   </button></td><tr>";
   echo "</form></div></table></div>";

现在是百万美元的问题,由于我不知道选择和订购了多少产品,我不知道我的 SESSION['cart_X']; 的最大值是多少 因此我不知道在我的表单上创建了多少输入字段。

我想要做的是成功处理我的 POST 数组中的每个值并将其输出到 $emailbody (需要处理订单并通过电子邮件发送)我尝试过使用foreach ($_POST as $entry){ print $条目。"
";}
但没有成功

这是代码..

if (isset($_POST['submit'])){
    $orderno = session_id();
    $contact = $_POST['Contact'];
    $telephone = $_POST['Telephone'];
    $address = $_POST['address'];
    $instructions = $_POST['instructions'];
    //Here I need to catch every POST variable (all items given as input fields in my form) values
    $to = "orders@t.co.za";
    $subject = "Order received via website";
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
    $headers .= 'To: Johan <schoeman.jj@gmail.com>'."\r\n";
    $headers .= 'From: Order via Web <orders@triumphant.co.za>'."\r\n";
    $emailbody = "Order number :".$orderno."<br>
        Contact Person: ".$contact."<br>
    Telephone number: ".$telephone."<br>
    Delivery Address: ".$address."<br>
    Instructions: ".$instructions."<br>
//Here I need to include every POST order item
4

1 回答 1

1

不要添加$num到输入的名称,而是创建一个数组:

function list_cart_items() {
    // .....
        <td><input type='text' name='order_item[]' value='".$num."' size='2' maxlength='2'>
        <td><input type='text' name='order_qty_item[]' value='".number_format($value,1)."' size='3' maxlength='3'>
        <td><input type='text' name='order_name_item[]' value='".$iget_row['name']."' size='30' maxlength='30'>
        <td><input type='text' name='order_value_item[]' value='R ".number_format(($iget_row['price']*$value), 2)."' size='6' maxlength='6'>

他们将在您的$_POST. 只需遍历它们。

$emailbody = "Order number :".$orderno."<br>
    Contact Person: ".$contact."<br>
    Telephone number: ".$telephone."<br>
    Delivery Address: ".$address."<br>
    Instructions: ".$instructions."<br>";

foreach($_POST['order_item'] as $key => $value) {
    // Add to $emailbody (edit line below to match your markup)
    $emailbody .= '' . $_POST['order_item'][$key] . ' ' . $_POST['order_qty_item'][$key] . '<br />'; //etc
}
于 2013-07-24T08:51:05.540 回答