[英]Call function inside CodeIgniter's controller using jquery / ajax
Can somebody please explain to me what is the right way to call a php function with jquery / ajax in Codeigniter. 有人可以向我解释在Codeigniter中用jquery / ajax调用php函数的正确方法是什么。 Right now, this code isn't working and i cant figure out whay.
现在,这段代码不起作用,我无法弄清楚。 Note that admin.php controler is inside admin map.
请注意,admin.php控制器位于管理映射中。 Thanks in advance
提前致谢
html code HTML代码
<form action="#" method="POST" id="change">
<input type="hidden" value="<?php echo $row->id_product; ?>" id="prod" >
<input type="submit" value="switch" >
</form>
<div class="resultdiv">
<?php echo $data; ?>
</div>
my function inside admin.php controller 我的函数在admin.php控制器里面
public function do_search(){
$id = $this->input->post('id');
return $id;
}
Jquery AJAX script Jquery AJAX脚本
$( "#change" ).submit(function() {
alert( "Change" );
var id = $('#prod').val();
$.ajax({
type:'POST',
url:'admin321/do_search',
data:{'id':id},
success:function(data){
$('#resultdiv').html(data);
}
});
});
Config / routes.php Config / routes.php
$route['admin/do_search'] = "admin_controller/admin/do_search";
I know that this is old post, but maybe someone will find this usefull. 我知道这是旧帖子,但也许有人会觉得这很有用。
I solve this problem by adding index.php
in url. 我通过在url中添加
index.php
来解决这个问题。 Even if the index.php
is hidden using rewrite. 即使使用重写隐藏了
index.php
。
$( "#change" ).submit(function() {
alert( "Change" );
var id = $('#prod').val();
$.ajax({
type:'POST',
url:'<?php echo base_url("index.php/admin/do_search"); ?>',
data:{'id':id},
success:function(data){
$('#resultdiv').html(data);
}
});
});
Maybe like this: 也许是这样的:
$( "#change" ).submit(function() {
alert( "Change" );
var id = $('#prod').val();
$.ajax({
type:'POST',
url:'<?php echo base_url("admin/do_search"); ?>',
data:{'id':id},
success:function(data){
$('#resultdiv').html(data);
}
});
});
You have to load this helper: 你必须加载这个帮手:
$this->load->helper('url');
@edit @编辑
$route['admin/do_search'] = "admin_controller/admin/do_search";
This code is unnecessary. 这段代码是不必要的。
I know this works. 我知道这很有效。 My routes file is the default.
我的路线文件是默认值。
I loaded CI URL helper in my controller __construct() function 我在我的控制器__construct()函数中加载了CI URL帮助器
$this->load->helper('url');
Here's my ajax: 这是我的ajax:
/*
*Ajax function to load confirmation page
*/
var formID=$("div form");
formID.submit(function(event){ //activated on submit event
event.preventDefault(); //stops page from reloading
$.ajax({
type:"POST",
url:'<?php echo site_url("plan/process")?>',
data:formID.serialize(),
success:function(data){
$("div #msg_area").html(data);
window.setTimeout(function(){parent.location.reload()},3000);
}
});
});
I have multiple controllers so it calls the specific one call plan and the \\n the function process. 我有多个控制器,因此它调用特定的一个呼叫计划和\\ n功能过程。 The process function one looks like this:
过程函数看起来像这样:
function process (){
$json_data = strtolower(json_encode($this->input->post()));
$res = array();
//Simple Error/success display...
$res = json_decode($this->plan->process_plan($json_data ),true);
if(array_key_exists('error',$res)){
$window = "warning";
$error=explode(":",$res['error']);
$result['message']="<h2><span class='color-dark'>Submission error:</span> ".$error[0]." </h2><p>".$error[1]."</p>";
}
else{
$window = "success";
$result['message'] = "<h2>Submission was a success</h2>";
}
echo $this->load->view("common/components/".$window,$result);
}
This works great for me. 这对我很有用。 Hopefully it helps.
希望它有所帮助。
in your routes file you have: admin321/do_search
NOT: admin/do_search
在您的路线文件中:
admin321/do_search
NOT: admin/do_search
You can also try using the absolute path: 您也可以尝试使用绝对路径:
`http://www.website.com/admin/do_search` or `http://localhost/admin/do_search`
in the ajax url parameter 在ajax url参数中
In the past, I have set up a route for the ajax request. 在过去,我已经为ajax请求设置了路由。 Something like this:
像这样的东西:
$route['admin/search/(:any)'] = 'admin_controller/admin/do_search/$1';
Then my ajax request would look like this: 然后我的ajax请求看起来像这样:
var prod = $('#prod').val();
$.ajax({
type: 'post',
url:'admin/search/'+prod
...
});
Or, you can grab the form action via jQuery and use that as your url. 或者,您可以通过jQuery获取表单操作并将其用作您的URL。
<form action="admin/search/123" method="post">
$.ajax({
type: 'post',
url: $('form').attr('action')
...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.