Codegniter - Internal error when acessing database - php

I'm using CodeIgniter to access the database, but everytime I run the code it gets an internal error. This is my jQuery code:
function showCadeiras() {
$.ajax({
type: "POST",
url: base_url + "api/teacher/getCadeiras",
data: {id: localStorage.user_id},
success: function(data) {
for(var i = 0; i < data.cadeiras_id.length; i++) {
var cadeira_id = data.cadeiras_id[i].cadeira_id;
$.ajax({
type: "POST",
url: base_url + "api/teacher/getCadeiraInfo",
data: {id: cadeira_id},
success: function(data) {
console.log(data);
},
error: function(data) {
console.log("error");
}
})
}
},
error: function(data) {
console.log(data);
}
});
}
Here is the code inside the Controller:
public function getCadeiras() {
$user_id = $this->post('id');
$this->load->model('UserModel');
$data["cadeiras_id"] = $this->UserModel->getCadeiras($user_id);
$this->response($data, parent::HTTP_OK);
}
public function getCadeiraInfo(){
$cadeira_id = $this->post('cadeira_id');
$this->load->model('UserModel');
$data["info"] = $this->UserModel->getCadeiraInfo($cadeira_id);
$this->response($data, parent::HTTP_OK)
}
Lastly, the Model code:
public function getCadeiras($id) {
$this->db->select("cadeira_id");
$this->db->where(array('user_id =' => $id));
$query = $this->db->get('professor_cadeira');
return $query->result_array();
}
public function getCadeiraInfo($id) {
$query = $this->db->get_where('cadeira', array('id =' => $id));
return $query->result_array();
}
Everytime I comment the second function (getCadeiraInfo) the code works without a problem, however it doesn't work with the second function. They are written in a similar way, I don't understand the error.

Related

Code igniter MVC ajax delete not working

why is this failing.
my view code
function Dispose(id){
if(confirm('Are you sure delete this data?'))
{
// ajax delete data to database
$.ajax({
url:"<?php echo site_url('Login/ajax_delete')?>" ,
type: 'post',
data: {'id' : id},
success: function () {
alert('ajax success');
},
error: function () {
alert('ajax failure');
}
});
}
}
controller code:
public function ajax_delete()
{
$id = $this->input->post('id');
$this->mod->delete_by_id($id);
}
model code:
public function delete_by_id($id)
{
$this->db->where('id', $id);
$this->db->delete('chemicalBottleInfo');
}
what i want to happen is to delete the item from chemicalbottleinfo
Your are passing id was wrong should not be quotes
function Dispose(id){
if(confirm('Are you sure delete this data?'))
{
// ajax delete data to database
$.ajax({
url:"<?php echo site_url('Login/ajax_delete')?>" ,
type: 'post',
data: {id : id},
success: function () {
alert('ajax success');
},
error: function () {
alert('ajax failure');
}
});
}
}
Another way:
Adding a check before deleting the id in the table.
Javascript:
function Dispose(id){
if(confirm('Are you sure delete this data?'))
{
// ajax delete data to database
$.ajax({
url:"<?php echo site_url('Login/ajax_delete/'+id)?>" ,
success: function () {
alert('ajax success');
},
error: function () {
alert('ajax failure');
}
});
}
}
Controller:
public function ajax_delete($id= '')
{
$id = $this->input->post('id');
if($id != ''){
$response = $this->usrExist($id, 'chemicalBottleInfo');
}
if($response == 1){
$this->mod->delete_by_id($id);
}
}
public function usrchk($id,$tableName) {
$qry = "SELECT count(*) as cnt from ".tableName." where id= ?";
$res = $this->db->query($qry,array($id))->result();
if ($res[0]->cnt > 0) {
return 1;
} else {
return 0;
}
}

Can not update data with ajax on codeigniter

I tried to update the data with ajax on php but it went wrong while ajax information has been successful but the data is not updated, I think the script is correct but do not want to update the data, what is wrong with my script ??
<input type="hidden" id="select_id" name="select_id" value="<?php echo $read_inbox['id_data']; ?>" />
$('[id^=delete_read_inbox]').click(function() {
if (confirm('You are sure to delete this message?')) {
var id = $("#select_id").val();
var url = base_url+'message/delete_inbox_read';
$.ajax({
url : url,
type: 'POST',
data: 'select_id='+id,
success: function(response) {
console.log('success');
},
error: function (request, jqXHR, textStatus, errorThrown) {
console.log(request.responseText);
}
});
} else {
}
});
Controllers
function delete_inbox_read() {
$this->Message->delete_ReadInbox();
redirect('user/message/inbox');
}
Models
function delete_ReadInbox() {
$update = $this->input->post('select_id');
$data = array(
'delete_pa_inbox' => 0
);
$this->db->where('id_Message', $update);
$this->db->update('tb_message', $data);
}
you are trying to POST 'id' from js and fetching 'select_id' on PHP side, hence its not working, change to:
...
var id = $("#select_id").val();
var url = base_url+'message/delete_inbox_read';
$.ajax({
url : url,
type: 'POST',
data: { 'id' : id },
success: function(response) {
console.log('success');
},
....
Controller:
function delete_inbox_read() {
//get the POST data
$select_id = $this->input->post('id'); //id not select_id
$this->Message->delete_ReadInbox($select_id);
//redirect('user/message/inbox'); //remove redirect
echo "done";
}
Model:
function delete_ReadInbox($select_id) {
$data = array(
'delete_pa_inbox' => 0
);
$this->db->where('id_Message', $select_id);
$this->db->update('tb_message', $data);
}

How to call a PHP function within a $.getJSON function?

A PHP function should return a specific number and stored it to the variable data[i]. How to access a PHP function within a $.getJSON function? I used a PHP CodeIgniter framework. Thank you!
`$.getJSON("<?php echo site_url('some_contoller/some_funcion');?>", function (data) {
for (var i = 0; i < data.length; i++) {
var data[i] = "<?php echo $this->some_PHP_function($with_parameter)?>";
}
}`
This is my PHP function:
public function get_report_pictures($report_id) {
$this->db->select('*');
$this->db->where('report_id', $report_id);
$query = $this->db->get('report');
$data = $query->result();
return $data;
}
in javascript file (*.js) you can't use php code.
you can use $.ajax() to call function in controller .
$.ajax({
type: "post",
url: "some_contoller/some_funcion",
dataType :'json',
success: function(response){
if(response.status=='success'){
response.data.forEach( function (item)
{
var x = item.number;
console.log(x);
});
}
},error:function (jqXHR, textStatus, errorThrown) {
console.log(textStatus);
}
});
in function you mentioned 'some_funcion'
echo json_encode(array(
'status'=>'failed',
'data' => 'number you say ')
);
exit();
it's my solution.i hope so that's worked.

CI: Get returned information in ajax response

I am working on a project with CI and Ajax. Since, I'm working with ajax after a long time, I am having some issues in debugging. I have written this code. In which i am sending data to to controller function login. Please guide me regarding how to check whether the data is reaching controller, and model. And also on how to return data from model to controller and from controller to view.
My Ajax code is as follows:
$('#login').click(function () {
if (($('#inputUname').val() === "") || ($('#inputPassword').val() === "")) {
alert('please username and password');
} else {
var data = {
'uname': $('#inputPassword').val(),
'pwd': $('#inputPassword').val()
};
$.ajax({
type: "POST",
url: base_url + "home/login",
data: data,
dataType: "json",
success: function (response)
{
alert('Ajax Success');
}, error: function () {
alert('Ajax Error');
}
});
}
});
Controller home.php Code is as follows:
public function login() {
$uname = $this->input->post('uname');
$pwd = $this->input->post('pwd');
$data['userinfo'] = $this->dis_model->check_user($uname,$pwd);
return $data;
}
and Model Dis_model.php code is as follows:
function check_user($uname, $pwd) {
$this->db->select('*');
$this->db->where('uname', $uname);
$this->db->where('pwd', $pwd);
$query = $this->db->get('users');
return $query->result();
}
All positive suggestions are welcomed.
Thanks in advance.
Change In Controller:
public function login() {
$uname = $this->input->post('uname');
$pwd = $this->input->post('pwd');
$data['userinfo'] = $this->dis_model->check_user($uname,$pwd);
echo $uname;
exit;
}
Change in ajax:
$('#login').click(function () {
if (($('#inputUname').val() === "") || ($('#inputPassword').val() === "")) {
alert('please username and password');
} else {
var data = {
'uname': $('#inputPassword').val(),
'pwd': $('#inputPassword').val()
};
$.ajax({
type: "POST",
url: base_url + "home/login",
data: data,
dataType: "json",
success: function (response)
{
alert(response);
}, error: function () {
alert('Ajax Error');
}
});
}
});
Same in modal
Change in Controller
public function login() {
$uname = $this->input->post('uname');
$pwd = $this->input->post('pwd');
$data['userinfo'] = $this->dis_model->check_user($uname,$pwd);
echo json_encode($data['userinfo']);
die;
}
Change in ajax
$('#login').click(function () {
if (($('#inputUname').val() === "") || ($('#inputPassword').val() === "")) {
alert('please username and password');
} else {
var data = {
'uname': $('#inputPassword').val(),
'pwd': $('#inputPassword').val()
};
$.ajax({
type: "POST",
url: base_url + "home/login",
data: data,
dataType: "json",
success: function (response)
{
alert(response);
}, error: function () {
alert('Ajax Error');
}
});
}
});

Simple ajax function in cakephp 2.x not working

I am new to cakephp and trying to implement AJAX . I have a view add.ctp in which I have written the following lines :
$('#office_type').change(function(){
var office_id = $('#office_type').val();
if(office_id > 0) {
var data = office_id;
var url_to_call = "http://localhost/testpage/officenames/get_office_names_by_catagory/";
$.ajax({
type: "GET",
url: url_to_call,
data = data,
//dataType: "json",
success: function(msg){
alert(msg);
}
});
}
});
And the function get_office_names_by_catagory() within OfficenamesController.php is:
public function get_office_name_by_catagory($type = '') {
Configure::write("debug",0);
if(isset($_GET['type']) && trim($_GET['type']) != ''){
$type = $_GET['type'];
$conditions = array("Officename.office_type"=> $type);
$recursive = -1;
$office_names = $this->Officename->find("all",array("conditions"=>$conditions,"recursive"=>$recursive));
}
$this->layout = 'ajax';
//return json_encode($office_names);
return 'Hello !';
}
But unfortunately, its not alerting anything ! Whats wrong ?
Could be caused by two issues:
1) In your js snippet, you are querying
http://localhost/testpage/officenames/get_office_names_by_catagory/.
Note the plural 'names' in get_office_names_by_category. In the PHP snippet, you've defined an action get_office_name_by_catagory. Note the singular 'name'.
2) You may need to set your headers appropriately so the full page doesn't render on an AJAX request: Refer to this link.
I think, you have specified data in wrong format:
$.ajax({
type: "GET",
url: url_to_call,
data = data, // i guess, here is the problem
//dataType: "json",
success: function(msg){
alert(msg);
}
});
To
$.ajax({
type: "GET",
url: url_to_call,
data: { name: "John", location: "Boston" }, //example
success: function(msg){
alert(msg);
}
});
You should specify the data in key:value format.
$('#office_type').change(function(){
var office_id = $('#office_type').val();
if(office_id > 0) {
var data = office_id;
var url_to_call = "http://localhost/testpage/officenames/get_office_name_by_catagory/"+office_id;
$.ajax({
type: "GET",
url: url_to_call,
success: function(msg){
alert(msg);
}
});
}
});
In your action
public function get_office_name_by_catagory($type = '') {
$this->autoRender = false;
Configure::write("debug",0);
if(!empty($type)){
$conditions = array("Officename.office_type"=> $type);
$recursive = -1;
$office_names = $this->Officename->find("all",array("conditions"=>$conditions,"recursive"=>$recursive));
}
$this->layout = 'ajax';
//return json_encode($office_names);
echo 'Hello !';
exit;
}
See what I have done is I have changed your request to function get_office_name_by_catagory, as there is one paramenter $type is already defined in the function, so if I have the request by /get_office_name_by_catagory/2 then you will find value in $type in action.
So no need to use $_GET and rest everything is fine!
Try this,
remove type from ajax and try.
$('#office_type').change(function(){
var office_id = $('#office_type').val();
if(office_id > 0) {
var data = office_id;
var url_to_call = yourlink +office_id;
**$.ajax({
url: url_to_call,
success: function(msg){
alert(msg);
}
});**
}
});
In your action
public function get_office_name_by_catagory($type = '') {
$this->autoRender = false;
Configure::write("debug",0);
if(!empty($type)){
$conditions = array("Officename.office_type"=> $type);
$recursive = -1;
$office_names = $this->Officename->find("all",array("conditions"=>$conditions,"recursive"=>$recursive));
}
$this->layout = 'ajax';
//return json_encode($office_names);
echo 'Hello !';
}

Categories