[英]Pass a value from Controller to jQuery CodeIgniter
This is Insert_model 这是Insert_model
class Insert_model extends CI_Model{
public function idp(){
$sql = sprintf("SELECT npd from `dosen` ORDER BY npd DESC LIMIT 1");
$query = $this->db->query($sql);
foreach ($query->result_array() as $key ) {
$npd1 = $key['npd'];
}
$kalimat1 = substr($npd1, 0,3);
return $kalimat1;
}
This is a function from the Register Controller: 这是来自寄存器控制器的功能:
class Register extends CI_Controller{
public $model;
public function __construct(){
parent::__construct();
$this->load->helper(['url','html']);
$this->load->database();
$this->load->model('Insert_model');
$this->model=$this->Insert_model;
}
public function index(){
$nomor= array('npd' => $this->model->idp());
echo json_encode($nomor);
}
}
This is how I access the $nomor value via Ajax: 这就是我通过Ajax访问$ nomor值的方式:
$("#status").on("change", function(){
var x = $("#status option:selected").attr("value");
if(x=='mahasiswa'){
$.ajax({
url:'<?php echo base_url();?>register/index',
type: 'POST',
dataType: 'json',
success : function(nomor){
$('#nomor').attr('placeholder',nomor.npd);
},
});
});
I want to be able to pass $nomor
from the Register Controller to use it on my jQuery file. 我希望能够从注册控制器传递
$nomor
以在我的jQuery文件中使用它。 I tried to use json_encode()
on the Controller and actually it didn't work. 我试图在Controller上使用
json_encode()
,但实际上不起作用。
Try with controller: Ref : https://www.codeigniter.com/userguide3/general/models.html 尝试使用控制器:Ref: https : //www.codeigniter.com/userguide3/general/models.html
$this->load->model('model_name');
$data['npd'] = $this->model_name->method();
echo json_encode($data);
Try This, 尝试这个,
public function index(){
$nomor= array('npd' => $this->model->idp());
echo json_encode($nomor);
exit();
}
i hope this will help you 我希望这能帮到您
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.