简体   繁体   English

在Codeigniter中的Ajax和控制器之间传递变量

[英]variable passing between ajax and controller in codeigniter

HTML : HTML

<input type="text" name="txt_uname" id="txt_uname"/>

Ajax : 阿贾克斯

<script type="text/javascript">

         $(document).ready(function(){
            $("#txt_uname").change(function(){
                 $("#message").html("<img src='images/ajax-loader.gif' width='20' height='20'  /> checking...");


            var cname=$("#txt_uname").val();

              $.ajax({
                    type:"post",
                    url:"money_c/nameget",
                    data:"txt_uname="+txt_uname,
                        success:function(data){
                        if(data==0){
                            $("#message").html("<font color='#55DF00'>Name Available</font>");
                        }
                        else{
                            $("#message").html("<font color='#FF0000' >Name Already taken</font>");
                        }
                    }
                 });

            });

         });

       </script>

controller : 控制器

function nameget()
    {
        $name= $this->input->post('txt_uname'); 
        $find=$this->money_m->nameget($name);

    }

model : 型号

function nameget($name)
    {
        $query=$this->db->query("SELECT * from customer3 where cusname='$name'");
          return $query->result();
    }

This code shows nothing as result.I want to pass variable from ajax to controller .Then find the value from model and pass to ajax.And i want show a message like in the code.plz help me.... 这段代码没有显示任何结果。我想将变量从ajax传递给控制器​​。然后从模型中找到值并传递给ajax。并且我想显示一条代码中的消息.plz帮我....

Define variable on JS: 在JS上定义变量:

var txt_uname = $("#txt_uname").val();

Model (if you need number of rows) : 模型(如果需要行数)

function nameget($name){
        $query = $this->db->query("SELECT * from customer3 where cusname='$name'");
          return $query->num_rows();
    }

echo value on controller: 控制器上的回显值:

echo $this->money_m->nameget($name);

It's recommended to use json_encode php function to pass data from server to client, and then later, decode that JSON data with some Jquery plugin. 建议使用json_encode php函数将数据从服务器传递到客户端,然后再使用一些Jquery插件对该JSON数据进行解码。
Use the console panel in chrome or firefox to debug errors. 使用chrome或firefox中的控制台面板来调试错误。

On your Controller 在您的控制器上

function nameget()
{
    $name= $this->input->post('txt_uname'); 
    echo $this->money_m->nameget($name);

}

on your model 在您的模型上

function nameget($name)
{
    $query=$this->db->query("SELECT * from customer3 where cusname='$name'");
      if($query->num_rows()>0)
      {
          return 1;
      }else{
             return 0;
           }
}

Jquery correction: jQuery校正:

<script type="text/javascript">

         $(document).ready(function(){
            $("#txt_uname").focusout(function(){
                 $("#message").html("<img src='images/ajax-loader.gif' width='20' height='20'  /> checking...");


            var txt_uname=$("#txt_uname").val();

              $.ajax({
                    type:"post",
                    url:"money_c/nameget",
                    data:"txt_uname="+txt_uname,
                        success:function(data){
                        if(data==0){
                            $("#message").html("<font color='#55DF00'>Name Available</font>");
                        }
                        else{
                            $("#message").html("<font color='#FF0000' >Name Already taken</font>");
                        }
                    }
                 });

            });

         });
</script>

**Controller correction:**


function nameget()
    {
        $name= $this->input->post('txt_uname'); 
        echo $this->money_m->nameget($name);

    }

Model correction 模型校正

function nameget($name)
{
    $query=$this->db->query("SELECT * from customer3 where cusname='$name'");
      if($query->num_rows()>0)
      {
          return 1;
      }else{
             return 0;
           }
}

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

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