Submit AngularJS form with CodeIgniter - json

Let me preface this by saying I'm new to AngularJS & CodeIgniter.
Trying to create a single page app. In the center it displays news stories. On the side is a form to submit new stories. I'm to the point where the stories display fine from the DB. But when I try to create new stories from the form on the side, it enters 0 into the DB. I assume I have an issue with my function in the model. And it's probably as basic as not having the information encoded correctly to pass between MVC.
index.php
<div class="span2" ng-controller="NewStoryCtrl">
<h4>New Story</h4>
<form name="newStory">
<label for="title">Title: </label>
<input type="text" name ="title" ng-model="news.title" required>
<label for="text">Text: </label>
<textarea type="text" name="text" ng-model="news.text"required></textarea>
<button ng-click="createNews()">Submit</button>
</form>
controller.js
function NewStoryCtrl($scope, $http) {
$scope.news = {title:$scope.title, text:$scope.text};
$scope.createNews = function(){
$http.post('/ci/index.php/news/create/',$scope.news);
};}
news.php (controller)
public function create() {
$this->news_model->set_news();
}
news_model.php (model)
public function set_news() {
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'time' => time(),
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
The stuff in the model is leftover from my initial CI news tutorial. That's why I assume the error is here.
What's the best way to pass the information from the controllers.js to the model?

As expected, my issue was with not getting the right type of data. Controller was expecting a variable, but in Angular controller I was passing it as JSON. I hadn't ever gone through decoding.
news.php (controller)
public function create() {
$data = json_decode(file_get_contents('php://input'), TRUE);
$this->news_model->set_news($data);
}
And then in the model, I just needed to pass it as a parameter to set_news(). I ended up changing some variable names, just for personal clarification.
*news_model.php*
public function set_news($data) {
$this->load->helper('url');
$slug = url_title($data['title'], 'dash', TRUE);
$retval = array(
'title' => $data['title'],
'slug' => $slug,
'time' => time(),
'text' => $data['text']
);
return $this->db->insert('news', $retval);
}

I haven't used Angular.js, but your data format is probably correct. For example, Backbone sends a single $_POST variable called model with json encoded data, as I have used it.
It is imperative that you use Firebug, Webdev or other tools to see what is going on when you try to do AJAX work like this; otherwise you will go crazy . Look at the variable being sent to your backend - it is probably described, an encoded single var you will need to collect, json_decode, CLEAN & VALIDATE and then use.

Related

Pass data from a query to a view

The problem I have is that I do not know how I can use the data of a query in a view if someone can give me a serious idea, the form I use is this but it gives me an error:
My Controller:
public function edit($id){
$datosProfesor = DB::table('profesor')->where('id', $id)->get();
$institucionDB = DB::table('institucion')->get();
$datos = array('datosProfesor' => $datosProfesor, 'institucionDB' => $institucionDB);
return view('profesorView/editarProfesor', $datos);
}
My view:
<div class="form-group">
<h4>Nombre</h4>
<input type="text" name="nombre_profesor" id="nombre_profesor" class="form-control" placeholder= 'Nombre' value="{{$datosProfesor['id']}}">
</div>
The error that appears to me is this:
Undefined index: id (View: /var/www/html/Konecte/resources/views/profesorView/editarProfesor.blade.php)
The get() method returns an Illuminate\Support\Collection containing the results where each result is an instance of the PHP StdClass object. You may access each column's value by accessing the column as a property of the object:
foreach ($datosProfesor as $profesor) {
echo $profesor->id;
}
If you want to get a single result then you can use find() method as:
$datosProfesor = DB::table('profesor')->find($id);
which is a short form of:
$datosProfesor = DB::table('profesor')->where('id', $id)->first();
Just one more tip for you that you can use PHP's compact() method for creating array containing variables and their values. So that you can get rid of line
$datos = array('datosProfesor' => $datosProfesor, 'institucionDB' => $institucionDB);
Your final code will look as:
public function edit($id){
$datosProfesor = DB::table('profesor')->find($id);
$institucionDB = DB::table('institucion')->get();
return view('profesorView/editarProfesor', compact('datosProfesor', 'institucionDB'));
}

How to populate dropdown from database laravel 5.x

Controller side:
$regs = Model::all('id','name');
return view('aview',compact('regs'));
View side:
{{ Form::select('id', $regs) }}
The dropdown gets rendered and populated but displays JSON objects such as {"id:1","name: Aname"} instead of displaying Aname and setting the post value to 1
Try this
In your controller
$regs = Model::pluck('name','id');
Keep your view same
Hope this will work
You can populate like this:
{!! Form::select('id', $regs->lists('name', 'id'), null, ['class' => 'form-control']) !!}
Form::select accepts four parameters:
public function select($name, $list = [], $selected = null, $options = []);
The name of the html field
the list of options
the selected value
an array of html attributes
You can generate the list by using
$regs = Model::all('id','name');
$plucked = $regs->pluck('name', 'id');
// $plcuked = ['id1' => 'name1', 'id2' => 'name2' ...]
And the blade code should look like this
{{ Form::select('name', $plucked, null, ['class' => 'form-control']); }}
I maybe making this problem a bit complicated but I think its worth using the plugin.
You can take the use of very popular plugin - Select2. This plugin of jQuery helps you to fetch data from server and populate the fetched data into our dropdown in minutes. Your code goes like this.
// Code in your Controller Method
$regs = Model::all();
$data = [];
foreach($regs as $reg) {
$data[] = [
'id' => $reg->id,
'text' => $reg->name
];
}
return json_encode(['items' => $data]);
// Code in your desired View
<select id="select_items"></select>
// Code in js
$('#select_items').select2({
ajax: {
url: '/example/api', // <--------- Route to your controller method
processResults: function (data) {
return {
results: data.items
};
}
}
});
You can also integrate search options using this plugins as it helps you to fetch results based on your search keywords (for more see Select2 Examples). Hope this helps you to solve your problem.

Generate link in cakephp using Html helper

I am trying to simply create a link in my controller using the Html helper and I get the below error although I have added the necessary helper:
Call to a member function link() on a non-object
public $helpers = array('Html', 'Form');
$url = $this->Html->link(
'',
'http://www.example.com/',
['class' => 'button', 'target' => '_blank']
);
You can use Helpers inside your view files but not inside your controller
http://book.cakephp.org/2.0/en/views/helpers.html#using-helpers.
For example in your index.ctp
echo $this->Html->link(
__('My link'),
'http://www.example.com/',
array('class' => 'button', 'target' => '_blank')
);
Enabling Html Helper in your Controller is same as in your code.
class ExamplesController extends AppController {
$helpers = array('Html', 'Form');
public function index() {
//
}
}
This is a good question. I think your a little confused with MVC and the separation of concerns the design pattern provides. Take a look (again) at how CakePHP implements MVC: http://book.cakephp.org/2.0/en/cakephp-overview/understanding-model-view-controller.html.
The important thing to remember is that your controllers should never be concerned with creating anchor tags. That's the job of your views. Since helpers are a way to keep your views DRY (Don't Repeat Yourself) having one thats sole responsibility is to create HTML elements is really handy. Views are dependent on controllers to determine what variables are set, what their value is, as well as what helpers are loaded. For more information on Helpers as well as components for controllers and behaviors for models check out http://book.cakephp.org/2.0/en/getting-started/cakephp-structure.html as well as each of their individual documentation pages:
Helpers - http://book.cakephp.org/2.0/en/views/helpers.html
Components - http://book.cakephp.org/2.0/en/controllers/components.html,
Behaviors - http://book.cakephp.org/2.0/en/models/behaviors.html.
Now that you have a better understanding of MVC, let's take a look at your specific issue. Your wanting to create a link in your controller. I assume it might be dynamic depending on some other variables so I'm going to roll with that.
A common problem that you can solve you want to show a login/logout link depending on if the user is already logged in.
In app/Controller/ExampleController.php
class ExampleController extends AppController {
public $components = array('Auth');
public $helpers = array('Html', 'Form');
public function beforeRender() {
parent::beforeRender();
//if Auth::user() returns `null` the user is not logged in.
if ($this->Auth->user() != null) {
$logInOutText = 'Log out';
$logInOutUrl = array('controller' => 'users', 'action' => 'login');
} else {
$logInOutText = 'Log in';
$logInOutUrl = array('controller' => 'users', 'action' => 'logout');
}
$this->set(compact('logInOutText', 'logInOutUrl'));
}
}
You can then so something simple in your view. In this case I'm choosing the default layout because I want the links in every rendered page. app/View/Layouts/default.ctp
<!-- More HTML above -->
<?php
// "Html" in `$this->Html` refers to our HtmlHelper. Note that in a view file
// like a `.ctp`, `$this` referes to the View object, while above in the
// controller `$this` refers to the Controller object. In the case
// `$this->Html`, "Html" would refer to a component. The same goes for Models
// and behaviors.
echo $this->Html->link($logInOutText, $logInOutUrl); // Easy!
?>
<!-- More HTML below -->
I hope this helps. I know it's a lot to put together at one time.
Though it is not good practice. However still if you need this you can use following code in your controller
App::uses('HtmlHelper', 'View/Helper');
$yourTmpHtmlHelper = new HtmlHelper(new View());
$url=$yourTmpHtmlHelper->link(
'',
'http://www.example.com/',
['class' => 'button', 'target' => '_blank']
);
This should work for cakephp 2.*
Thanks

Cakephp 3 & Ajax, send $.get additional data

I try to send custom data using $.get from jquery but cakephp 3 does not recognize the variables.
Here is my function in controller:
public function view($cat,$id,$page){
$comments = $this->Comments->find('all')->where(['category =' => $cat, 'AND' => ['category_id =' => $id]])->order(['comments.created' => 'DESC'])
->contain([
'Reports' => function($q){return $q->where(['user_id =' => $this->Auth->user('id')]);},
'Chars' => function($q){ return $q->select(['id','name','class','race','level','guild_id', 'user_id'])
->contain(['Guilds' => function($q){ return $q->select(['id','name']);
}]);
}])->limit(3)->page($page);
if($cat == 'Videos'){
$subject = $this->Comments->Videos->get($id);
}
$category = $cat;
$this->set(compact('subject','comments','category'));
}
}
And here's the .js
$('.more').click(function(){
$.get($(this).attr('href'),{cat:'Videos',id:'44',page:'2',function(data){
$('.coms').empty().append(data);
});
return false;
});
And the link:
<?= $this->Html->link('More', ['controller' => 'Comments','action' => 'view'], ['class' => 'btn btn-primary more']) ?>
The fix values in the .js is for the test, it works if I send the data in the link with $.get(href) but I want to know how to pass custom data in the $.get request.
Thank you for your help
CakePHP doesn't magically map query string parameters to method arguments, that's not even supported by routes.
You can access query string paramters via the request object
$this->request->query('cat');
See also http://book.cakephp.org/3.0/en/controllers/request-response.html#query-string-parameters
The solution
Hi, I did a little api to get the data I need from a json array.
So I created an ApiController to do all the ajax requests I need. Here is test example:
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\ORM\TableRegistry;
class ApiController extends AppController
{
public function test($id)
{
$UsersTable = TableRegistry::get('Users');
$users = $UsersTable->find()->where(['id <' => $id]);
$this->set(compact('users'));
}
}
?>
I want to get the list of all my users from an ajax call (that's the worst idea ever but it's just for the test ;))
Now I need to enable json for this view. For this, I need to go in config/routes.php to add this:
Router::scope('/api', function($routes){
$routes->extensions(['json']);
$routes->connect('/test/*', ['controller' => 'Api', 'action' => 'test', '_ext' => 'json']);
});
Now when I go on http://localhost/mywebsite/api/test/100, I have a json array of all the users with an id < 100, ready to get pulled by an ajax script.
'_ext' => 'json'
means you don't have to go on api/test.json to display the json array
Now on a random view in a random controller, I put a button to try all this and I call my javascript file to do my ajax request.
<button class="test">Test button</button>
<?= $this->Html->script('test', ['block' => true]) ?>
Now in my test.js:
$(document).ready(function(){
var value = 100;
$('.test').click(function(){
$.get('/api/test/' + value, function(data){
console.log(data);
});
});
});
I click the button and the log shows an array with all the users I wanted in my console when I push F12 (on chrome).
Hope this will help someone

zend framework 2, return the rendered contents of a view inside a JSON Model

I am trying to create a JsonModel with an item in the variables 'html' containing the current rendered view. I would like to add this code to an event:
rather than this method: How to render ZF2 view within JSON response? which is in the controller, I would like to automate the process by moving it to an Event
I have the strategy in my module.config.php:
'strategies' => array(
'ViewJsonStrategy',
)
I have set up a setEventManager in the controller:
$events->attach(MvcEvent::EVENT_RENDER, function ($e) use ($controller) {
$controller->setRenderFormat($e);
}, -20);
Is this the best event to attach it to? would the RENDER_EVENT be better?
Now I would like to change the render of the page based on !$this->getRequest()->isXmlHttpRequest(), (commented out for debug)
public function setRenderFormat($e)
{
//if(!$this->getRequest()->isXmlHttpRequest())
//{
$controller = $e->getTarget();
$controllerClass = get_class($controller);
//Get routing info
$controllerArr = explode('\\', $controllerClass);
$currentRoute = array(
'module' => strtolower($controllerArr[0]),
'controller' => strtolower(str_replace("Controller", "", $controllerArr[2])),
'action' => strtolower($controller->getEvent()->getRouteMatch()->getParam('action'))
);
$view_template = implode('/',$currentRoute);
$viewmodel = new \Zend\View\Model\ViewModel();
$viewmodel->setTemplate($view_template);
$htmlOutput = $this->getServiceLocator()->get('viewrenderer')->render($viewmodel, $viewmodel);
$jsonModel = new JsonModel();
$jsonModel->setVariables(array(
'html' => $htmlOutput,
'jsonVar1' => 'jsonVal2',
'jsonArray' => array(1,2,3,4,5,6)
));
return $jsonModel;
//}
}
Strangely, (or not) this code works and produces the $jsonModel, however is doesn't overwite the normal HTML view with the json, but the same code (without the event) in a controller method, overwrites perfectly.
p.s Is there a better method to do the whole concept?
p.p.s how can I obtain the current View Template from within the controller, without resorting to 8 lines of code?
Thanks in advance!
Aborgrove
you are returning the view model from an event I thinks this doesn't have any effect in current viewmanager view model, fetch the current viewmodel from viewmanager and call setTerminal(true). or replace the created jsonmodel using the viewmanager