Codeigniter inline edit not working - codeigniter

I created the following code, but do not run. Can you help?
My Controller
public function updateDb()
{
//$this->load->model('user_m');
$this->page_m->inline($_POST['file_description']);
return;
}
My Model
public function inline( $file_description, $id ){
$this->db->set($data)->where($this->$id, $page['file_description'])->update($this->file);
}
Form
<?php echo $file->file_description?>
Jquery
function showEdit(file_description) {
$(file_description).css("background","#FFF");
}
function save(file_description) {
$(file_description).css("background","#FFF url(<?php echo base_url('assets/img/loaderIcon.gif');?>loaderIcon.gif) no-repeat right");
$.ajax({
url: "<?php echo base_url()?>index.php/admin/page/updateDb",
type: "POST",
id : '<?php echo $page->id;?>',
name : 'file_description'
});
}

Looks like you're not passing the ID to your model which it requires.
public function inline( $file_description, $id ){...}
$this->page_m->inline($_POST['file_description'], $_POST['id']);
You could use $this->input->post() though...

Related

I can not return Data in ajax

I can not get the response from the controller with Ajax,what should I do?:
$("document").ready(function(){
$("#send").click(function(e){
e.preventDefault();
var question = $("input[name=question]").val();
$.ajax({
type: "POST",
url : "/Admin/question/store?token={{$t}}",
data : {'question':question},
success : function(data){
alert(data);
console.log(data);
}
});
});
});
and in the controller I just want to return a string for testing:
public function store(Request $request)
{
return "success";
}
For you to get the response in AJAX you must print the data, is it being print after the return? if not, try :
public function store(Request $request)
{
echo "success";
}
I solved it,
First problem was URL,I changed it to:
{!! route('store') !!}
And did not know to write my route in api!!!! instead of web

BadMethodCallException in Builder.php line 2405: in laravel

I am new to laravel and facing an issue,I am calling a controller via ajax call ,it is calling controller but when controller is calling model it is giving
"BadMethodCallException in Builder.php line 2405:" error.Following is my code
AJAX:
$.ajax({
url: "<?php echo url('master/add-car-to-db');?>",
method : "post",
data : completeData ,
success : function (data){
data=parse.JSON(data);
if(data.success == 1){
window.location="<?php echo url('master/add-car-view');?>";
}
},
error: function(data,status){
alert(JSON.stringify(data));
}
});
Controller:
public function postAddCarToDb(Request $request){
$result = Master::addNewCar($request);
echo $result;
die;
if ($result > 0) {
echo json_encode(Array('success' => 1));
} else {
echo json_encode(Array('success' => 0));
}
}
Model:
public static function addNewCar(Request $request){
return "success"
}
Route:
Route::controller('master', 'MasterController');
Thanks in advance
Reverse routing is deprecated in laravel 5 so on.
Route::controller('master', 'MasterController');
use
Route::post('master/add-car-to-db','MasterController#postAddCarToDb');
then in Controller
if you still want to use reverse routing you can
$.ajax({
url: "<?php echo action(YourController#yourAction) ;?>",
method : "post",
data : completeData ,
success : function (data){
data=parse.JSON(data);
if(data.success == 1){
window.location="<?php echo url('master/add-car-view');?>";
}
},
error: function(data,status){
alert(JSON.stringify(data));
}
});

How to use x-editable with CodeIgniter?

I would like to understand using x-editable in my CodeIgniter first project. I tried to read x-editable docs but I'm beginner in JavaScript too so I can't understand
I make simple controller to collect data from JavaScript but I didn't complete it or data not updated in database.
$('#username').editable({
type: 'text',
pk: 1,
url: '/post',
title: 'Enter username'
});
How to get submitted data in controller or model to process database update query
I want to passing data submitted from x-editable to model to update it in database.
You can follow this simple steps
Assume that $userId = 5 ; $username = "admin";
Consider you html look like this
<a type="text" name="username" onclick="setEditable(this);" data-pk="<?php echo $userId ;?>" data-placeholder="Enter Username" data-name="username" data-type="text" data-url="<?php echo site_url();?>user/updateUserName" data-value="<?php echo $username ;?>" data-prev="admin" data-title="Enter Username"><?php $username; ?></a>
In Javascript code write following
$.fn.editable.defaults.mode = 'inline';
function setEditable(obj) {
$(obj).editable({
emptytext: $(obj).attr('data-value'),
toggle: 'dblclick',
mode: 'inline',
anim: 200,
onblur: 'cancel',
validate: function(value) {
/*Add Ur validation logic and message here*/
if ($.trim(value) == '') {
return 'Username is required!';
}
},
params: function(params) {
/*originally params contain pk, name and value you can pass extra parameters from here if required */
//eg . params.active="false";
return params;
},
success: function(response, newValue) {
var result = $.parseJSON(response);
$(obj).parent().parent().find('.edit-box').show();
$(obj).attr('data-value', result.username);
$(obj).attr('data-prev', result.username);
},
error: function(response, newValue) {
$(obj).parent().parent().find('.edit-box').hide();
if (!response.success) {
return 'Service unavailable. Please try later.';
} else {
return response.msg;
}
},
display: function(value) {
/*If you want to truncate*/
var strName = strname !== '' ? strname : $(obj).attr('data-value');
var shortText = '';
if (strName.length > 16)
{
shortText = jQuery.trim(strName).substring(0, 14).split("").slice(0, -1).join("") + "...";
}
else {
shortText = strName;
}
$(this).text(shortText);
}
});
$(obj).editable('option', 'value', $(obj).attr('data-value'));
}
In Controller site
<?php
class User extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function updateUserName()
{
$this->load->model('user_model');
if ($this->input->is_ajax_request()) {
$valueStr = $this->input->get_post('value') ? $this->input->get_post('value') : '';
$new_nameStr = trim($valueStr);
$result_arr['username'] = $new_nameStr;
$userId = $this->input->get_post('pk') ? $this->input->get_post('pk') : '';
$data['username'] = $new_nameStr;
$result_arr['username'] = $new_nameStr;
$this->user_model->userUpdateFunction($data, $userId);
}
echo json_encode($result_arr);
exit;
}
}
You can change editable mode , i have set inline only
First of all, this question is about AJAX and JavaScript/jQuery, not Codeigniter.
Basically, the code that you wrote is about posting data with AJAX. First, you need to create a controller and model, then you can post data with AJAX. I'm adding a sample code:
Controller file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sample extends CI_Controller {
function __construct() {
parent::__construct();
$this ->load ->model('modelfolder/sample_model');
}
public function index() {
$this->sample_model->sampleFunction();
}
}
Model File:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sample_model extends CI_Model {
function sampleFunction() {
$data = array('fieldName' => $this->input->post('userName', TRUE));
$this->db->where('id', $this->input->post('userId', TRUE));
$this->db->update('tableName', $data);
return true;
}
}
routes.php File:
$route['demoPost'] = 'controller_folder/sample';
View File's HTML part:
<form id="sampleForm">
<input type="text" name="userId" />
<input type="text" name="userName" />
</form>
View File's AJAX part:
$(document).ready(function(){
$("#sampleForm").submit(
function(){
$.ajax({
type: "POST",
url: "<?php echo site_url('demoPost'); ?>",
data: $("#sampleForm").serialize(),
});
});
});

filtering column table using select box ajax in codeigniter

i want to filtering data on the table using select box. When user choose value from select box then column on the table will filtered. I mean the table just show data that contain value on select box that user choose. Can anyone help me
this is my ajax :
<script>
$("#inputJenis").on('change',function(){
if($("#inputJenis").val() != 'Ganjil'){
$.ajax({
type: "POST",
url:'<?php echo base_url()?>search/filter',
data:'selectvalue='+$('#inputJenis').val(),
cache: false,
success: function(data){
$(#tableData).html(data);
},
error: function(data){
//return false;
}
});
});
});
</script>
this is my controller :
public function filter()
{
$this->load->helper('url');
$this->load->model('filter_model');
$this->filter_model->getData();
$data = $this->filter_model->getData();
echo json_encode($data);
}
this is my model :
public function getData($type)
{
$this->db->select('jenis');
$query = $this->db->get('tahunajaran');
return $query->result();
echo json_encode($query);
}
This code didn't work. help me please
Try this:
AJAX:
<script type="text/javascript">
var base_url='<?php echo base_url(); ?>';
</script>
<script>
$("#inputJenis").change(function(e){
if($("#inputJenis").val() != 'Ganjil'){
$.ajax({
type: "POST",
url:base_url+'search/filter',
data:'selectvalue='+$('#inputJenis').val(),
cache: false,
success: function(data){
$(#tableData).html(data);
},
error: function(data){
//return false;
}
});
});
});
</script>
Controller
public function filter()
{
$this->load->helper('url');
$this->load->model('filter_model');
$this->filter_model->getData();
$selectValue=$this->input->post('selectvalue')
$data = $this->filter_model->getData($selectValue);
echo json_encode($data);
}
Model
public function getData($selectValue)
{
$this->db->select('jenis');
//Change it with your Field Name
$query = $this->db->where('SomeField',$selectValue)->get('tahunajaran');
return $query->result_array();
}

Joomla component: controller not returning json

Why is my controller not returning my data in JSON format? Note that I am developing my component on Joomla 3.1.1.
/hmi.php
//Requries the joomla's base controller
jimport('joomla.application.component.controller');
//Create the controller
$controller = JControllerLegacy::getInstance('HMI');
//Perform the Request task
$controller ->execute(JRequest::setVar('view', 'hmimain'));
//Redirects if set by the controller
$controller->redirect();
/controller.php
class HMIController extends JControllerLegacy
{
function __construct()
{
//Registering Views
$this->registerTask('hmimain', 'hmiMain');
parent::__construct();
}
function hmiMain()
{
$view =& $this->getView('hmimain','html');
$view->setModel($this->getModel('hmimain'), true);
$view->display();
}
public function saveHMI()
{
echo 'Testing';
$this->display();
}
}//End of class HMIController
/controllers/properties.json.php
class HMIControllerProperties extends JController
{
function __construct()
{
$this->registerTask(' taskm', 'taskM');
parent::__construct();
}
function taskM()
{
$document =& JFactory::getDocument();
// Set the MIME type for JSON output.
$document->setMimeEncoding('application/json');
// Change the suggested filename.
JResponse::setHeader('Content-Disposition','attachment;filename="json.json"');
echo json_encode('Hello World');
// Exit the application.
Jexit();
}
}
JQuery function calling the joomla task
var request = $.ajax({
dataType:"json",
url:"index.php?option=com_hmi&task=properties.taskm&format=json",
type:"POST",
data:{propPage: "ABC"},
beforeSend: function (){
$("#loading_Bar").css("display","block");
}
});// dot ajax
When I use the above ajax settings the request fails. However if I change the datatype property to text, and remove the format=json from the url, I get html instead of json.
Can some one point out what I'm doing wrong?
Further investigation to my problem i concluded that the componenet was not triggering the desired task becuse of the following code in my /hmi.php
$controller ->execute(JRequest::setVar('view', 'hmimain'));
So I modified my /hmi.php as follows
//Requries the joomla's base controller
jimport('joomla.application.component.controller');
// Create the controller
$controller = JControllerLegacy::getInstance('HMI');
$selectedTask = JRequest::getVar( 'task');
if ($selectedTask == null)
{
//This will allow you to access the main view using index?option=com_hmi
//and load the "default" view
$controller->execute( JRequest::setVar( 'view', 'hmimain' ) );
}
else
{
//Will execute the assigned task
$controller->execute( JRequest::getVar( 'task' ) );
}
// Redirect if set by the controller
$controller->redirect();
then created the /controllers/properties.json.php file with the following code
class HMIControllerProperties extends JControllerLegacy
{
function myMethod()
{
$model = $this->getModel('hmimain');
$dataToolboxItems =& $model->getToolboxItems();
echo json_encode($dataToolboxItems);
//JExit();
}
}//End of class HMIController
then i'm calling the task from jquery as follows:
var request = $.ajax({
dataType:"json",
//task=properties.mymethod will access the subcontroller within the controllers folder
//format=json will by access the json version of the subcontroller
url:"index.php?option=com_hmi&task=properties.mymethod&format=json",
type:"POST",
data:{propPage: "ABC"},
beforeSend: function (){
$("#loading_Bar").css("display","block");
}
});
In your ajax request try changing to this format:
dataType:'json',
url: 'index.php',
data: {option: 'com_hmi', task: 'properties.task', format: 'jason', propPage: 'ABC' },
type:'POST',
.....
Another thing is in the controller file add the Legacy:
HMIControllerProperties extends JControllerLegacy
And I don't think you need this lines, for me it works without them
$document->setMimeEncoding('application/json');
JResponse::setHeader('Content-Disposition','attachment;filename="json.json"');

Resources