学习如何使用AJAX与CodeIgniter [英] Learning how to use AJAX with CodeIgniter

查看:108
本文介绍了学习如何使用AJAX与CodeIgniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个非常简单的控制器,像这样:

  class front extends Controller {

public function __construct()
{
parent ::控制器();
}

public function index()
{
// nothing!
}

public function test(){

$ someNumber = $ this-> input-> post('someNumber');

if($ someNumber == 12){return TRUE; }

}

}

这可能写得更好,哈哈。



我想知道的是 - 如何使用JavaScript提交表单中的数字(我会担心以后的验证和模型),我应该怎么写我的test()函数,以便它返回的东西可读的JavaScript(我假设返回TRUE可能不工作,也许XML或JSON或类似的东西?),如何访问数据与JavaScript?



我知道有像jQuery这样的框架可以帮助到这一点,但现在我只想在最简单的层面上理解它,所有的教程和指南到目前为止,我发现的东西对我来说太深了。在jQuery中的一个例子或任何东西也将是好的。



感谢很多:)

解决方案

并通过javascript重新捕获该信息:

  public function test(){
$ somenumber = $ this-> input-> post('someNumber');
if($ somenumber == 12){
printNumber is 12;
} else {
printNumber is not 12;
}
}

您的javascript可能看起来像这样:

  var xhr; 
xhr = new XMLHTTPRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
//这是返回信息的地方
alert('Status:'+ xhr.status +'Response:'+ xhr.responseText);
}
}

xhr.open('POST','/ front / test');
xhr.setRequestHeader('Content-Type','application / x-www-form-urlencoded');
xhr.send('someNumber = 12');

上面的代码没有考虑到浏览器的具体问题,但应该运行在firefox / ie7至少我相信。



这里有一个jQuery的例子:

  var options = {
'someNumber':12
}

$ .post('/ front / test',options,function(data){
alert(data); //应该打印Number is 12
});


It's kind of embarassing that I find it so difficult to learn JavaScript, but ..

Let's say I have a really simple controller like this:

class front extends Controller {

    public function __construct()
    {
        parent::Controller();   
    }

    public function index()
    {
        //nothing!
    }

    public function test () {

        $someNumber = $this->input->post('someNumber');

        if ($someNumber == 12) { return TRUE; }

    }

}

Yes, that could probably be written better, haha.

What I want to know is - how could I use JavaScript to submit a number in a form (I'll worry about validation and models later), how should I write my test() function so that it returns something readable by the JavaScript (I'm assuming return TRUE probably wouldn't work, perhaps XML or JSON or something like that?), and how do I access the data with the JavaScript?

I know there are frameworks like jQuery that will help with this, but right now I'd just like to understand it at the simplest level and all the tutorials and guides I've found so far are way too in depth for me. An example in jQuery or whatever would be good too.

Thanks a lot :)

解决方案

you would just print it out basically, and re-capture that information via javascript:

public function test() {
    $somenumber = $this->input->post('someNumber');
    if ($somenumber == 12) {
        print "Number is 12";
    } else {
        print "Number is not 12";
    }
}

your javascript might look something like this:

var xhr;
xhr = new XMLHTTPRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        // this is where the return information is
        alert('Status: '+xhr.status+' Response: '+xhr.responseText);
    }
}

xhr.open('POST', '/front/test');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send('someNumber=12');

the code above doesn't take into account browser specific issues, but should run on firefox/ie7 at least i believe.

here's a jQuery example of all the above:

var options = {
    'someNumber' : 12
}

$.post('/front/test', options, function(data) {
    alert(data); // should print "Number is 12"
});

这篇关于学习如何使用AJAX与CodeIgniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆