$http.post not working inside ng-click function in Angular Js - php

I'm developing a project with Anjular js and Codeigniter 3. I have faced some issues with routing in angular js. I have a view page with a list of details of users and inside that list there is a tag with ng-click="myName()". I have declared this function inside my js file and this function works fine.
<div class="container" ng-controller="ListController">
<div class="row">
<div class="col-lg-12 text-center">
<h1>Starter Template</h1>
<p class="lead">Complete with pre-defined file paths that you won't have to change</p>
<div class="row">
<div class="col-sm-6 col-md-4 wow zoomIn" data-wow-delay="0.5s" ng-repeat="items in artists">
<div class="thumbnail">
<img ng-src="{{settings.base_url}}/application/assets/images/profile/{{items.shortname}}.jpg" alt="...">
<div class="caption">
<h3>{{items.name}}</h3>
<p>{{items.bio}}</p>
<p>Button Button</p>
</div>
</div>
</div>
</div>
</div>
</div>
Inside this function "myName()" I have written $http.post for getting a function inside the controller(Codeiginter controller). I'm using auth.php as controller and functionOne() is the function to retrive.
var base_url_abs = window.location.origin;
var base_url = base_url_abs+'/AngularExPack/Angular_Bootstrap_CI_1/';
var base_url_controller = base_url_abs+'/AngularExPack/Angular_Bootstrap_CI_1/index.php/';
myApp.controller('ListController', ['$scope', '$http', 'settings', function($scope, $http) {
$http.get('application/datas/data.json').success(function (data){
$scope.artists = data;
});
$scope.myName = function() {
alert("hai1");
/*
$http.post(base_url_controller+'auth/functionOne/').success(function (response){
$scope.data = response
console.log(response);
//alert($scope.data);
});*/
/**/ $http({
method : 'POST',
url : base_url_controller+'auth/functionOne/',
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
$scope.data = response
console.log($scope.data);
alert($scope.data);
});
}
}]);
When i run this, an error is occured and showing like this inside the console
But i'm getting the correct result when i click on this link and also if i give base_url path inside the href inside a tag.
Button Button
But what i need is inside this myName function i have to access the controller function functionOne() using $http using angular js. Is that possible.?
If anyone knows please help me to find out the solution in ng-click. Correct me if i written wrong code.
Thank you.

you should use
$http({
method : 'GET',
url : base_url_controller+'auth/functionOne/',
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
$scope.data = data
console.log($scope.data);
alert($scope.data);
});

Related

Ajax and codeigniter framework issues

I have a problem with AJAX and CodeIgniter, I want to get total count of users in database but there is no data found.
my controller:
public function TotalUsers()
{
if($this->input->post("action")=='GetTotalUsers')
{
$this->load->model("usersmodel");
$totalcount=$this->usermodel->GetTotalUserCount();
echo $totalcount;
}
}
my model:
public function GetTotalUserCount()
{
$query = $this->db->get("users");
return $query->num_rows();
}
My html and jquery:
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-yellow">
<div class="inner">
<h3 id="totaluser"></h3>
<p>Total Users</p>
</div>
<div class="icon">
<i class="ion ion-person-add"></i>
</div>
More info <i class="fa fa-arrow-circle-right"></i>
</div>
</div>
<script>
$(document).ready(function () {
GetTotalUsersCount();
function GetTotalUsersCount() {
var action='GetTotalUsers';
$.ajax({
url:"<?php echo base_url()?>Users/TotalUsers",
method:"post",
data:{action:action},
success:function (data) {
$("#totaluser").html(data)
}
})
}
})
</script>
I think you might be sending it encoded in JSON while the controller is expecting it serialized, although I'm not sure.
Run var_dump() on $this->input->post("action").
If that doesn't give GetTotalUsers, then that's you're problem. You'll need to either set the controller to accept raw post data and decode as json, serialize the data instead of using a javascript object (JSON), or if possible, just remove the action conditional because currently it doesn't look useful as shown.
I suspect that url: is not evaluating to the correct value. There are a couple syntax errors (missing semi-colons) too. Try this JavaScript. Use your browser's "Developer Tools" to examine the headers to see where the post "goes" and that "action" is being posted.
$(document).ready(function () {
GetTotalUsersCount();
function GetTotalUsersCount() {
$.ajax({
url: '<?php echo base_url("Users/TotalUsers"); ?>',
method: "post",
data: {action: 'GetTotalUsers'},
success: function (data) {
$("#totaluser").text(data);
}
});
}
});
I'm curious why you use ajax and don't simply determine and pass the total count to the initial loading of the view?
1.Use Ctrl+Shift+I (or navigate to Current Page Control > Developer >
Developer Tools )or( use right click on your page and then select inspect
element)
2.Go to "Network" tab
3.Find your ajax request in "Name Path" column
4.Click on the specific ajax link

How to pass the button value in ajax url and display the json response in bootstrap panel body?

Laravel View code:
<div class="container" style="display:none;" id="panel1">
<div class="panel panel-default">
<div class="panel-heading" style="color:#0000FF" data-toggle="tooltip" title="Gateways!">
<input type="button" value="Haghway Lite" style="background-color:transparent;border:none;width:100%;height:100%;" name="button1" id="button1"><font size="5"></font>
</div>
<div class="panel-body" align="center">
<img src="haghwaylitecontroller.jpg" width="100" height="100">
</div>
</div>
</div>
This my view code.
Script(ajax):
<script type="text/javascript">
$(document).ready(function() {
$("#button1").click(function(){
var button1 = $(this).val();
if(button1) {
$.ajax({
url: "{{url('panelview').'/'}}"+button1,
type: "GET",
dataType: "json",
success:function(data) {
$.each(data, function(key,value) {
$("#panel1").append('<div "'+ value.panel1 +'"></div>');
});
//alert("success");
}
});
}else{
alert("failed");
}
});
});
In this script code, I calling the ajax function with the button id and trying to pass the value of button to be redirected to the mentioned url and the returned json response to be displayed on the panel body. Here if I am trying to print the button value it alerts NaN
Route:
Route::get('panelview/{button1}','viewController#newProduct');
Controller:
public function newProduct($button1)
{
$users=addModel::select('Desc')->where('Pname',$button1 )->first();;
return json_encode($users);
}
The controller code should fetches the correspoding record based on the value of button and return the response.

server side pagination with angular and Php

I am having some problem with angularJs , while trying to do server side pagination.
Here is the HTML:
<div class="body-container detail-box">
<div class="table">
<div ui-grid="gridOptions" ui-grid-pagination class=""></div>
</div>
</div>
JS File:
module.exports = managerShowEditController;
},{}],12:[function(require,module,exports){
var managerIndexController = function($scope, $http, $resource, $stateParams, uiGridConstants, gridPaginSorting, config) {
// some code
}
The error is :
I am unable to use 'uiGridConstants, gridPaginSorting and config'
any idea whats going wrong ?
Add the package name to your app.js file. For Example : 'ui.grid'

use session made from ajax inside blade laravel 4.2

hi so im making a session via ajax when a certain button is clicked here is the code
function cModal(id)
{
alert(id);
$.ajax({
method: 'post',
url: 'http://localhost/dummy/app/putSession.php',
data: {
'dID': id
},
success: function()
{
alert(id);
}
});
}
it is successfull because it alerts the id inside the success:function()
in my putSession.php file here is how i did it
<?php
session_start();
$tId = $_POST['dID'];
$_SESSION["sID"] = $tId;
exit();
?>
but the thing is i can't access it inside my blade file im calling it like this
<div class="modal-content">
<div class="row rowMarg">
<?php session_start(); ?>
#if(Session::has('sID'))
{{ Session::forget('sID') }}
<h6>Information.</h6>
#else
<h6>File details not found.</h6>
#endif
</div>
</div>
it just goes to the file details not found
any idea what im doing wrong? thanks in advance!
use laravel session like
$request->session()->put('user', 'value');
$request->session()->get('user', 'value');
$request->session()->push('user.teams', 'developers');
for more https://laravel.com/docs/4.2/session

ionic infinite scrolling with ionic and php data

I am new to Ionic and working with project where I need to use infinite scrolling. I have searched for it but I didn't get satisfied answer. All solutions has static data which I don't want. I want to data from php file which is in server.
Controller
.controller('MylistCtrl', function ($scope, $http, $ionicLoading, $state, myService) {
$scope.items = {};
myService.getMyTime('37.0000', '35.3213', '2', '3', '2015').then(function (response) {
console.log(response);
$scope.items = response;
});
})
.factory('myService', function ($http, $q) {
return {
getMyTime: function (lat,lng,gmt,method,year) {
var deferred = $q.defer();
$http({
url: base_url + 'mylist.php',
method: "GET",
params: {lat: lat, lng: lng, GMT: gmt, Method: method, Year: year},
}).success(function (response, status) {
if (typeof response.data == 'object') {
deferred.resolve(response.data);
} else {
deferred.reject(response.data);
}
}).error(function () {
deferred.reject(response.data);
})
return deferred.promise;
}
}
});
HTML
<ion-view class="full-bg>
<ion-content>
<ion-list >
<ion-item ng-repeat="item in items">
<div class="row">
<div class="col col-30 month">
<div class="day">
<h1>{{item.day}}</h1>
<h2>{{item.month}}</h2>
</div>
<div class="year">{{item.year}}</div>
</div>
<div class="col col-45 time">{{item.Fajr}}</div>
<div class="col col-25 alarm-icon"><i class="icon ion-ios-alarm"></i><i class="icon ion-checkmark-round"></i></div>
</div>
</ion-item>
</ion-list>
<ion-infinite-scroll on-infinite="loadMore()" distance="1%"></ion-infinite-scroll>
</ion-content>
Please help me with this. Thank you in advance.
You are not using Ionic Infinite Scroll directive in your view. Include it. And $http service, returns a promise by default. Why are you creating a new defer object?

Categories