从控制器 codeigniter 调用 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5864797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling javascript function from controller codeigniter
提问by user735399
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???
我正在使用 codeigniter 开发网站。我有一个包含添加按钮和文本框的表单。一旦用户输入数据,我必须检查它是否存在于数据库中,如果是,则在页面中生成一个动态文本框,否则会提醒用户。我已经编写了 javascript 来生成动态文本框。我的问题是如何检查数据库???如何从 javascript 调用控制器或从控制器调用 javascript 函数???
回答by Atticus
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.
首先——我们将使用 jQuery 的原生 POST 函数。
Create a function inside your controller that you want to access, my suggestion is to prefix the function name with "ajax_"
在您要访问的控制器中创建一个函数,我的建议是在函数名称前加上“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:
这是您简单的 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');
}
}
);
}
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.
第二个参数{ username: name }
是您的帖子值的位置,这里的术语“用户名”是键,名称是传入的值。所以这是一个通常会随帖子消息一起发送的帖子键值对。
The variable response
being passed into the callback function is the echo given back by your controller. Communication made extremely easy.
response
传递给回调函数的变量是控制器返回的回声。沟通变得非常容易。
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.
简单是惊人的,虽然我只让你处理 php 返回 0 或 1,但你可以返回非常高级的 json 对象,你可以用它来驱动整个前端程序。
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 :)
这将返回给您一个完美的 json 数据集,您可以将其与任何面向对象的方法一起使用。我到处都用这个,你很快就会确定:)
Good luck man! Feel free to contact w/ any questions about expanding responses past simple 0 or 1 echos
祝你好运!如有任何关于扩展响应超过简单 0 或 1 回声的问题,请随时联系
回答by morgar
You can do it by making an ajax call to a php page in the server that checks if data exists in db. If you are using jquery, you can do it in an easier way, here you can find very good examples: http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/
您可以通过对服务器中的 php 页面进行 ajax 调用来检查 db 中是否存在数据。如果你使用 jquery,你可以用更简单的方式来做,在这里你可以找到很好的例子:http: //net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls -with-jquery/
If you don't, you can do it anyway, with some more lines of code.
如果你不这样做,你无论如何都可以用更多的代码行来完成。