Php 在CodeIgniter中使用Ajax分配数据

Php 在CodeIgniter中使用Ajax分配数据,php,jquery,ajax,codeigniter,Php,Jquery,Ajax,Codeigniter,这是购物车视图 <div id="cart" > <div id="text"> <?php $cart_check = $this->cart->contents(); // If cart is empty, this will show below message.

这是购物车视图

        <div id="cart" >

            <div id="text">
                <?php  $cart_check = $this->cart->contents();

                    // If cart is empty, this will show below message.
                    if(empty($cart_check))
                    {
                        echo 'To add products to your shopping cart click on "Add to Cart" Button';
                    }  ?> </div>

            <table id="table" border="0" cellpadding="5px" cellspacing="1px" class="table table-hover" style="padding: 0px;margin: 0px;text-align: center">
                <?php
                    // All values of cart store in "$cart".
                    if ($cart = $this->cart->contents()): ?>
                        <tr id= "main_heading">
                            <td width="5%">Serial</td>
                            <td width="30%">Name</td>
                            <td width="20%">Price</td>
                            <td width="10%">Qty</td>
                            <td width="25%">Amount</td>
                            <td width="10%">Cancel Product</td>
                        </tr>
                        <?php
                        // Create form and send all values in "shopping/update_cart" function.
                        echo form_open('index.php/cart/update_cart');
                        $grand_total = 0;
                        $i = 1;

                        foreach ($cart as $item):
                            //   echo form_hidden('cart[' . $item['id'] . '][id]', $item['id']);
                            //  Will produce the following output.
                            // <input type="hidden" name="cart[1][id]" value="1" />

                            echo form_hidden('cart[' . $item['id'] . '][id]', $item['id']);
                            echo form_hidden('cart[' . $item['id'] . '][rowid]', $item['rowid']);
                            echo form_hidden('cart[' . $item['id'] . '][name]', $item['name']);
                            echo form_hidden('cart[' . $item['id'] . '][price]', $item['price']);
                            echo form_hidden('cart[' . $item['id'] . '][qty]', $item['qty']);
                            ?>
                            <tr>
                            <td>
                                <?php echo $i++; ?>
                            </td>
                            <td>
                                <?php echo $item['name']; ?>
                            </td>
                            <td>
                                $ <?php echo number_format($item['price'], 2); ?>
                            </td>
                            <td>
                                <?php echo form_input('cart[' . $item['id'] . '][qty]', $item['qty'], 'maxlength="3" size="1" style="text-align: center"'); ?>
                            </td>
                            <?php $grand_total = $grand_total + $item['subtotal']; ?>
                            <td>
                                $ <?php echo number_format($item['subtotal'], 2) ?>
                            </td>
                            <td>

                                <?php
                                    // cancle image.
                                ?>
                                <a href="<?php echo base_url() ;?>index.php/cart/remove/<?php echo $item['rowid']?>">
                                    <img src="<?php echo base_url(); ?>assets/images/cart/cart_cross.jpg" width='25px' height='20px'>
                                </a
                                <?php
                                    //                                        echo anchor('index.php/cart/remove/' . $item['rowid'], $path); ?>
                            </td>
                        <?php endforeach; ?>
                        </tr>
                        <tr class="info">
                            <td colspan="2" align="left"> <b>Order Total: $ <span style="font-weight: bold"> <?php

                                            //Grand Total.
                                            echo number_format($grand_total, 2); ?></span></b></td>

                            <?php // "clear cart" button call javascript confirmation message ?>
                            <td colspan="4" align="right"><input type="button" class ='fg-button teal' value="Clear Cart" onclick="clear_cart()">

                                <?php //submit button. ?>
                                <input type="submit" class ='fg-button teal' value="Update Cart">
                                <?php echo form_close(); ?>

                                <!-- "Place order button" on click send "billing" controller  -->
                                <!--                                    <input type="button" class ='fg-button teal' value="Place Order" onclick="window.location = 'index.php/cart/billing_view'">-->
                                <button id="placeorder" type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo" style="margin-left: 5px">Place Order</button>
                            </td>

                        </tr>
                    <?php endif; ?>
            </table>
        </div>
所有这些都可以使用ajax

<script>
    $(function(){
        $( "#placeorder" ).click(function(event){
            event.preventDefault();
            var htmlString = $("#mail_table").html();
            $.ajax({
                type : 'post',
                url : '<?php echo base_url();?>index.php/cart/send_mail',
                data : $('#form').serialize() + "table" + htmlString
            });
        });  
    });
</script>
但我犯了个错误

遇到一个PHP错误

严重性:通知

消息:未定义索引:表

文件名:controllers/cart.php

行号:144


有什么想法吗?

问题在于您认为ajax中的这行代码。使用此代码
不发送参数。您的表单id也是
mailform

data : $('#form').serialize() + "table" + htmlString
是的

data : $('#mailform').serialize() + "&table=" + encodeURIComponent(htmlString)

查看购物车控制器的第144行。您尚未与我们共享它。
$message=$\u POST['table']//第144行
就是这样,PHP不同意您的意见,我无法理解@scrowler。你能帮我澄清一下吗???这个错误是不言自明的,关于堆栈溢出有成千上万的问题与之相关。您是否尝试搜索该错误消息?请检查此项。你错过了什么吗
alert(htmlString)
我想你是在用ajaxya发送整个表格的html。这就是我真正需要的,为什么你不为此创建一个单独的视图
     public function send_mail()
      {
        $name = $_POST['name'];
        $mail = $_POST['mail'];
        $mob = $_POST['mob'];
        $price = $_POST['price'];
        $msg = $_POST['msg'];

        //                $to =
        $to = 'mail@gmail.com';


        $to = 'my@gmail.com';
        $subject = 'Enquiry from ' .$name;
        $message = $_POST['table']; //Line 144
        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        //                    $headers .= 'From: '.$_REQUEST['email'];
        if(mail($to, $subject, $message, $headers))
        {

        }

    }
data : $('#form').serialize() + "table" + htmlString
data : $('#mailform').serialize() + "&table=" + encodeURIComponent(htmlString)