pass data to controller through ajax in Code ignit

2019-07-28 05:16发布

问题:

i am beginner in a code igniter and jquery. i have a textbox in which if user type some thing it displays the data in tooltip from the database.. here in my situation what i want is ... i have a texbox named called "bill no".. what i want is if a user enter bill no in a text box it displays the data against the bill no.. i dont know how can i pass data from controller to view in ajax and then after pass how to show in tooltip here is my code

Bill No:<?php echo form_input($bill_no); ?>
<input type="hidden" class="hiddenUrl"><span class="checkbillno" data-trigger="manual"


     data-title="bill no" data-content="here i want to display results"></span> 

my javascript

<script type="text/javascript">

    $(document).ready(function(){

        $('#bill_no').blur(function(){

            if( $('#bill_no').val().length >= 3 )
                {
                  var bill_no = $('#bill_no').val();
                  getResult(bill_no); 
                }
            return false;
        })
        function getResult(billno){
            var baseurl = $('.hiddenUrl').val();
            $('.checkbillno').addClass('preloader');
            $.ajax({
                url : baseurl + 'Controller/checkBillNo/' + billno,
                cache : false,
                success : function(response){
                    $('.checkbillno').removeClass('preloader');
                    if(response == 'userOk')
                        $('.checkbillno').removeClass('preloader');
                    if(response == 'userOk') $('.checkUser').removeClass('userNo').addClass('userOk');
                    else $('.checkUser').removeClass('userOk').addClass('userNo');

                }
            })
        }
    })

here what i am doing is that it simply check the bill no in database and if it is available in the database then it adds the class "userok" otherwise "userfalse".. and here what i want is display data as well as against the bill no..i know how to do this in model but dont know how can i pass from controller to ajax and then it shows the result in tooltip

my controller

function checkBillNo($billno){
    $this->load->model('returnModel');
    $query = $this->returnModel->checkBillNo($billno);

    if($query == 1 ) 
        $data['result'] = $query; //how can i pass this result into view in tooltip... (span class)

    else 
        echo 'userNo';

}

回答1:

A useful way to manipulate data is to use json in your datatypes:

function getResult(billno){
     var baseurl = $('.hiddenUrl').val();
     $('.checkbillno').addClass('preloader');
     $.ajax({
          url : baseurl + 'Controller/checkBillNo/' + billno,
          cache : false,
          dataType: 'json',
          success : function(response){
               $('.checkbillno').attr('data-content', response.toolTip);
          }
     })
}

Now your js function expects a json encoded array as a response, so in your controller you could do something like the below:

$forToolTip = '';
foreach($query->result() as $row){
    $forToolTip .= $row->key;
}

$result['toolTip'] = $forToolTip;
echo json_encode($result);

Alter variables to match your concept