Javascript-two-level interaction between thinkphp and ajax implementation drop-down list

Source: Internet
Author: User
Table Structure cId category idcName category name gid parent level id this is my HTML page & amp; lt; selectname & quot; gType & quot; id & quot; gType & quot; & amp; gt; {code ...} ajax code & amp; lt; script & amp; gt ;$ (& #039; # gType & #039 ;). click (function () {code ...}}); & amp; lt; script & amp; gt; thinkph... table Structure
CId category id
CName category name
Gid parent id
This is my HTML page
{$ Val. cName}

Ajax code
Script
$ ('# Gtype'). click (function (){

$(this).change(function(){    var objectModel = {};    var   value = $(this).val();   var   type = $(this).attr('id');    objectModel[gType] =value;    $.ajax({        cache:false,        type:"POST",        url:"/index.php/Goods/goodsAdd",        dataType:"json",        data:objectModel,        timeout:30000,        success:function(data){            $("#lables").empty();            var count = data.length;            var i = 0;            var b="";               for(i=0;i
  
   "+data[i].cName+"";               }            $("#lables").append(b);        }    });   });
  

}
);

Script
Thinkphp receiving page
Public function goodsAdd (){

    $model=M("category");            $rs=$model->where("pid=0")->select();    $this->assign("rs",$rs);    $this->display();    $result = array();    $cate =$_POST['gType'];    var_dump($cate);    $result = M('category')->where(array('pid'=> $cate))->field('cId,cName')->select();    var_dump($result);    $this->ajaxReturn($result,"JSON");        }

Where is the error? ajax is just getting started.

Reply content:

Table Structure
CId category id
CName category name
Gid parent id
This is my HTML page
{$ Val. cName}

Ajax code
Script
$ ('# Gtype'). click (function (){

$(this).change(function(){    var objectModel = {};    var   value = $(this).val();   var   type = $(this).attr('id');    objectModel[gType] =value;    $.ajax({        cache:false,        type:"POST",        url:"/index.php/Goods/goodsAdd",        dataType:"json",        data:objectModel,        timeout:30000,        success:function(data){            $("#lables").empty();            var count = data.length;            var i = 0;            var b="";               for(i=0;i
  
   "+data[i].cName+"";               }            $("#lables").append(b);        }    });   });
  

}
);

Script
Thinkphp receiving page
Public function goodsAdd (){

    $model=M("category");            $rs=$model->where("pid=0")->select();    $this->assign("rs",$rs);    $this->display();    $result = array();    $cate =$_POST['gType'];    var_dump($cate);    $result = M('category')->where(array('pid'=> $cate))->field('cId,cName')->select();    var_dump($result);    $this->ajaxReturn($result,"JSON");        }

Where is the error? ajax is just getting started.

What is your normal PHP code segment? Why display and ajaxReturn ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.