简体   繁体   中英

how to pass new jquery value to CI controller using ajax

First of all, I wanted to display data from the database to a table in VIEW one at a time then if there are changes the user will just have to double click the table and input the new value. here is the screenshot:

I'm done with the displaying and editing, what I'm trying to do next is to SAVE the new data/values so that I can pass it to the controller.

here is my code for getting data in the database and displaying it to the table using jquery by clicking the button MOVE ON..

<script type="text/javascript">
                    var leads = Array();
                    var lead_count = 0;
                    <?php $count = 0; foreach($agent_leads as $info):?>
                        leads[<?php echo $count; ?>] = {"fullname": "<?php echo $info['fullname'] ?>",
                                                        "gender" : "<?php echo $info['gender'] ?>",
                                                        "address": "<?php echo $info['address'] ?>",
                                                        "city" : "<?php echo $info['city']; ?>",
                                                        "state" : "<?php echo $info['state']; ?>",
                                                        "zipcode" : "<?php echo $info['zipcode']; ?>",
                                                        "email": "<?php echo $info['email'] ?>"
                    };
                    <?php $count++; endforeach; ?>
                    // console.log(lead_count)
                    // console.log(leads[lead_count])
                    if(lead_count == 0)
                    {
                        var append = '';
                        append += '<tr>';
                        append += '<td><div contenteditable>'+leads[lead_count].fullname+'</div></td>';
                        append += '<td><div contenteditable>'+leads[lead_count].gender+ '</div></td>';
                        append += '<td><div contenteditable>'+leads[lead_count].address+'</div></td>';
                        append += '<td><div contenteditable>'+leads[lead_count].city+   '</div></td>';
                        append += '<td><div contenteditable>'+leads[lead_count].state+  '</div></td>';
                        append += '<td><div contenteditable>'+leads[lead_count].zipcode+'</div></td>';
                        append += '<td><div contenteditable>'+leads[lead_count].email+  '</div></td>';
                        append += '<td><input type="text" id="status"/></td>';
                        append += '<td><input type="number" id="qty"/></td>';
                        append += '<td><input type="text" id="comment"/></td>';
                        append += '<td><button class="btn btn" id="sub" type="submit">Submit</button></td>';
                        append += '</tr>';

                        $('#leads_info').html(append);
                    }

                    $(document).on('click', '#move_on', function(){
                        var order = Number($(this).attr('data-order')) + 1;
                        var append = '';
                        append += '<tr>';
                        append += '<td>'+leads[order].fullname+'</td>';
                        append += '<td>'+leads[order].gender+'</td>';
                        append += '<td>'+leads[order].address+'</td>';
                        append += '<td>'+leads[order].city+'</td>';
                        append += '<td>'+leads[order].state+'</td>';
                        append += '<td>'+leads[order].zipcode+'</td>';
                        append += '<td>'+leads[order].email+'</td>';
                        append += '<td><input type="text" id="status"/></td>';
                        append += '<td><input type="number" id="qty"/></td>';
                        append += '<td><input type="text" id="comment"/></td>';
                        append += '<td><button class="btn btn-success" id="sub" type="submit">Submit</button></td>';
                        append += '</tr>';
                        $(this).attr('data-order', order);
                        $('#leads_info').html(append);
                    });

then here is my ajax code..

$(document).on('click', '#sub', function(){
                        var infos = Array();
                        infos['i'] = {
                                    lead:leads[lead_count],
                                    status: $('#status').val(),
                                    qty: $('#qty').val(),
                                    comment: $('#comment').val()
                                }
                        $.ajax({
                            url: window.location + '/sales_report',
                            type: 'POST',
                            data: infos,
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.log('error');
                            },
                            success: function (result) {
                                console.log('success');
                            }

                        });
                    });

but when I click Submit to pass all the data to the controller I get "error" in the console.log() I'm new to this jquery and ajax. I don't know much on how to pass array from view to controller but I know a little on how to get data from the Model->Controller

How to pass new values in tables to the controller?

如果看到错误,请打开开发人员控制台,转到网络页面,然后在/ sales_report页面上看到错误。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM