简体   繁体   English

通过Code点火器中的Ajax将数据传递到控制器

[英]pass data to controller through ajax in Code igniter

i am beginner in a code igniter and jquery. 我是代码点火器和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”的texbox。用户在文本框中输入帐单号,它将根据帐单号显示数据。我不知道如何从控制器传递数据以在ajax中查看,然后传递后如何在工具提示中显示,这是我的代码

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 我的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 在这里,我正在做的是,它只是检查数据库中的帐单号,如果它在数据库中可用,那么它将添加类“ userok”,否则为“ userfalse” ..在这里,我想要的是显示数据以及针对我不知道如何在模型中执行此操作,但是不知道如何从控制器传递给ajax,然后在工具提示中显示结果

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

}

A useful way to manipulate data is to use json in your datatypes: 处理数据的一种有用方法是在数据类型中使用json:

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: 现在,您的js函数期望以json编码的数组作为响应,因此在您的控制器中,您可以执行以下操作:

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

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

Alter variables to match your concept 更改变量以符合您的概念

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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