dongzhan0624 2013-07-24 08:43
浏览 50
已采纳

如何在表单提交时将未知数量的POST变量处理成电子邮件消息

I am new to php so here goes...

I am trying to output all items ordered from my SESSION variables into input values with a function so that I can process them with a form submit. Now the input fields are created successfully and all ok. here is the code

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>";

Here is the code of the actual form creation with submit button

    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>";

Now comes the million dollor question , since I do not know how many products have been selected and ordered , I do not know what the maximum value is for my SESSION['cart_X']; thus I do not know how many input fields have been created on my form.

What I am trying to do is to process each value in my POST array successfully and output that into a $emailbody (Order needs to be processed and emailed) I have tried playing around with the foreach ($_POST as $entry){ print $entry."
";}
but no success

here is the code ..

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' . "
";
    $headers .= 'Content-type: text/html; charset=iso-8859-1'."
";
    $headers .= 'To: Johan <schoeman.jj@gmail.com>'."
";
    $headers .= 'From: Order via Web <orders@triumphant.co.za>'."
";
    $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
  • 写回答

1条回答 默认 最新

  • doulaobi7988 2013-07-24 08:51
    关注

    Dont add $num to the name of the input, but create an array:

    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'>
    

    They you will receive those variables as arrays in your $_POST. Just loop through them.

    $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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)