Abdulla Nilam
Abdulla Nilam

Reputation: 38672

Assign data with Ajax in CodeIgniter

This is cart view

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

image

After click on placeholder button(above code) it will open bootstrap model.

Code of that is

        <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" style="text-align: left">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="exampleModalLabel">Tour Inquire Form</h4>
                    </div>
                    <?php
                        $grand_total = 0;
                        // Calculate grand total.
                        if ($cart = $this->cart->contents()):
                            foreach ($cart as $item):
                                $grand_total = $grand_total + $item['subtotal'];
                            endforeach;
                        endif;
                    ?>
                    <form action="<?php echo base_url();?>index.php/cart/send_mail" method="POST" id="mailform">
                        <div class="modal-body">
                            <div class="form-group">
                                <label for="recipient-name" class="control-label">Name:</label>
                                <input type="text" class="form-control" name="name">
                            </div>
                            <div class="form-group">
                                <label for="recipient-name" class="control-label">E-Mail</label>
                                <input type="email" class="form-control" name="mail">
                            </div>
                            <div class="form-group">
                                <label for="recipient-name" class="control-label">Contact Number</label>
                                <input type="number" class="form-control" name="mob">
                            </div>
                            <div class="form-group">
                                <label for="recipient-name" class="control-label">Total Value of Your Product</label>
                                <input type="text" class="form-control" name="price" value="<?php echo $grand_total;?>" readonly>
                            </div>
                            <div class="form-group">
                                <label for="message-text" class="control-label">Message:</label>
                                <textarea class="form-control" id="message-text" rows="5" name="msg"></textarea>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <input type="button" class="btn btn-default" data-dismiss="modal" value="Close">
                            <input type="submit" class="btn btn-primary" name="submit" value="Send Mail">
                        </div>
                    </form>
                </div>
            </div>
        </div>

image2

After user click on Send Mail, I need to send mail get data(html codes) from some other div(Mentioned below, Name mail_table) and data should send to my mail controller.

        <div id="mail_table" style="display: none">
            <table cellpadding="6" cellspacing="1" style="width:100%" border="0">

                <tr>
                    <th>QTY</th>
                    <th>Product Name</th>
                    <th style="text-align:right">Item Price</th>
                    <th style="text-align:right">Sub-Total</th>
                </tr>
                <?php $i = 1; ?>
                <?php foreach ($this->cart->contents() as $items): ?>
                    <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
                    <tr>
                        <td><?php echo $items["qty"] ?></td>
                        <td>
                            <?php echo $items['name']; ?>
                            <?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
                                <p>
                                    <?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
                                        <strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
                                    <?php endforeach; ?>
                                </p>
                            <?php endif; ?>
                        </td>
                        <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
                        <td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
                    </tr>
                    <?php $i++; ?>
                <?php endforeach; ?>
                <tr>
                    <td colspan="2"> </td>
                    <td class="right"><strong>Total</strong></td>
                    <td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
                </tr>
            </table>
        </div>

all works fine with 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>

but in controller when I assign data to

     public function send_mail()
      {
        $name = $_POST['name'];
        $mail = $_POST['mail'];
        $mob = $_POST['mob'];
        $price = $_POST['price'];
        $msg = $_POST['msg'];

        //                $to =
        $to = '[email protected]';


        $to = '[email protected]';
        $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))
        {

        }

    }

But I'm getting error

A PHP Error was encountered

Severity: Notice

Message: Undefined index: table

Filename: controllers/cart.php

Line Number: 144

any idea???

Upvotes: 0

Views: 891

Answers (1)

Saty
Saty

Reputation: 22532

Problem is this line of code in your view ajax. With this code table not send in your parameter. Also your form id is mailform

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

It would be

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

Upvotes: 2

Related Questions