从控制器codeigniter调用javascript函数 [英] Calling javascript function from controller codeigniter

查看:84
本文介绍了从控制器codeigniter调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用codeigniter开发网站。我有一个包含添加按钮和文本框的窗体。一旦用户输入数据,我必须检查它是否存在于数据库中,如果是在页面中生成动态文本框,否则警报用户。我写了javascript来生成动态文本框。我的问题是如何检查数据库???如何从JavaScript调用控制器或从控制器调用JavaScript函数?

解决方案

这实际上比你期望的容易得多..



首先,我们将使用jQuery的本地POST函数。



在你想要访问的控制器中创建一个函数,我的建议是在函数名前加上ajax _



下面是一个控制器函数的例子:

  function ajax_lookUpUsername(){
$ username = $ this-> input-> post('username');
$ this-> db-> where('username',$ username);
$ query = $ this-> db-> get('accounts');
if($ query-> num_rows()> 0){
echo 0;
} else {
echo 1;
}
}

这里是您的简单onclick javascript函数:

  function lookUpUsername(name){
$ .post(
'http:// yourwebsite / controller / ajax_lookUpUsername' ,
{username:name},
function(response){
if(response == 1){
alert('username available');
} else {
alert('username taken');
}
}
);
}

第二个参数 {username:name} code>是你的post值将在哪里,username这里的术语是键,name是传递的值。所以这是一个post键值对,通常会发送一个post消息。 / p>

传入回调函数的变量 response 是控制器返回的回声。通信非常容易。



简单是惊人的,而我只有你处理php返回0或1,你可以返回非常高级的json对象,你可以整个前端程序。



对于更高级的响应,您可以从控制器阵列以这种方式回显:

  echo json_encode($ array_of_data); 

,这将返回一个完美的json数据集,您可以使用任何面向对象的方法。我使用这个全部,你很快就会肯定:)



祝你好运!随时可以联系w /任何关于通过简单的0或1回合展开回复的问题


I am developing site using codeigniter.I have a form which contains add button and textbox. once the user enters the data i have to check whether it exist in database,if yes generate a dynamic textbox in the page else alert user. I have written javascript to generate dynamic textbox. My question is how to check in database??? how to call controller from javascript or to call javascript function from controller???

解决方案

This is actually so much easier than you expect.. and you will start to use this allover your developments once you see how great it is!

So first off -- we're going to use jQuery's native POST function.

Create a function inside your controller that you want to access, my suggestion is to prefix the function name with "ajax_"

So here's an example of the controller function:

function ajax_lookUpUsername(){
    $username = $this->input->post('username');
    $this->db->where('username', $username);
    $query = $this->db->get('accounts');
    if ($query->num_rows() > 0){
       echo 0;
    } else {
       echo 1;
    }
}

and here's your simple onclick javascript function:

function lookUpUsername(name){
    $.post( 
        'http://yourwebsite/controller/ajax_lookUpUsername',
         { username: name },
         function(response) {  
            if (response == 1) {
               alert('username available');
            } else {
               alert('username taken');
            }
         }  
    );
}

the second parameter { username: name } is where your post values will go, the term "username" here is the key, name is the value passed in. So this is a post key-value pair that would normally get sent with a post message.

The variable response being passed into the callback function is the echo given back by your controller. Communication made extremely easy.

The simplicity is amazing, while I only had you deal with php returning 0 or 1, you can return very advanced json objects that you can power an entire front-end program with.

For more advanced responses you can echo from your controller array's this way:

echo json_encode($array_of_data); 

and this will return to you a perfect json dataset that you can use with any object oriented approach. I use this allover, you will soon too im sure :)

Good luck man! Feel free to contact w/ any questions about expanding responses past simple 0 or 1 echos

这篇关于从控制器codeigniter调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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