php - 在 codeigniter 中使用 AJAX 从 View 调用 Controller 函数并从数据库中检索行

标签 php mysql codeigniter

我试图通过对 Controller 函数进行 AJAX 调用来从数据库中检索行。我没有错误消息,代码正确执行到 $("#msgbox").html("Type the name of someone or something...");

我认为 Controller 未从 View 中正确调用。下面是我的 MVC 代码。

我的观点:welcome_message.php

<div id="centersearch">
    <script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
var start=/^((?!PART).)*$/
var word=/^((?!PART).)*$/

$("#contentbox").live("keyup",function() 
{
var content=$(this).text(); //Content Box Data
var go= content.match(start); //Content Matching @
var name= content.match(word); //Content Matching @abc
var dataString = 'searchword='+ name;
//If @ available
if(go.length>0)
{
$("#msgbox").slideDown('show');
$("#display").slideUp('show');
$("#msgbox").html("Type the name of someone or something...");
//if @abc avalable
if(name.length>0)
{
$.ajax({
type: "POST",
url: "<?php $this->load->helper('url'); echo base_url();?>index.php/Welcome/ ?>", // Database name search 
data: dataString,
cache: false,
success: function(data)
{
$("#msgbox").hide();
$("#display").html(data).show();
}
});
}
}
return false();
});

//Adding result name to content box.
$(".addname").live("click",function() 
{
var username=$(this).attr('title');
var old=$("#contentbox").html();
var content=old.replace(word," "); //replacing @abc to (" ") space
$("#contentbox").html(content);
var E="<a class='red' contenteditable='false' href='#' >"+username+"</a>";
$("#contentbox").append(E);
$("#display").hide();
$("#msgbox").hide();
});
});
</script>

我的 Controller :Welcome.php

 public function search()
{
  $this->load->database();  
         //load the model  
         $this->load->model('select');  
         //load the method of model  
         $data['s']=$this->select->search();  
          $this->load->view('welcome_message', $data); 

}

我的模型:Select.php

public function search($datastring)
   {  

$query = $this->db->select('*')
        ->from('country')
        ->where('from', $datastring)
        ->get();
        return $query;
}

最佳答案

你可以在你的构造函数中调用你的助手

function __construct() {
        parent::__construct();
        $this->load->helper('url');
    }

你的ajax url是

  url: "<?php echo base_url();?>index.php/Welcome/search?>",

关于php - 在 codeigniter 中使用 AJAX 从 View 调用 Controller 函数并从数据库中检索行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30637002/

相关文章:

php - 我是将常量放入数据库还是使用文件来保存它

php - 为 Android 构建一个简单的音乐流应用程序

javascript - 单击提交按钮不起作用

php - 如何使用 PHP 生成 JSON 数据?

php - PDO 绑定(bind)参数错误

php - 我如何为 imagemagick 提供图像库路径

javascript - PHP-CodeIgniter : How to get the corresponding html table row values to be deleted by Javascript

javascript - 多个 Jquery 倒计时显示 NaN

php - 通过excel上传数据到Db

php - 将 phpgrid 与 codeigniter 集成