2016-04-03 13 views
0

Bir PHP betiğine erişmek için AngularJS kullanıyorum ancak sadece belirli bir işlevi çağırmak istiyorum. UpdatePost ve deletePost işlevinin altındaki app.js komut dosyasında bir düğme tıklatılarak çağrılır. Fonksiyonları ayrı dosyalara ayırdım ve bunları ayrı ayrı almak için köşeli fonksiyonun http fonksiyonunu kullandım ve hepsi iyi çalıştı ama şimdi onları işlevler içinde saklamak istiyorum. Php dosyası ama onları nasıl arayacağımı bilmiyorum bireysel olarak?Bir php komut dosyasında belirli bir işlevi çağırmak için AngularJS nasıl kullanılır?

İşte benim app.js komut

var app = angular.module('adminPanel', ['ngRoute', 'ngAnimate']) 

.config(['$routeProvider', 
    function($routeProvider) { 
    $routeProvider. 
     when('/dashboard', { 
     templateUrl: 'includes/main.php', 
     controller: 'MainCtrl' 
     }). 
     otherwise({redirectTo: '/dashboard'}) 
    }]) 

.controller('MainCtrl', ['$scope', '$http', function($scope, $http) { 
    getPosts(); 
    function getPosts() { 
     $http.post('functions.php').success(function(data) { 
      $scope.posts = data; 
     }); 
    } 
    $scope.updatePost = function(post) { 
     $http.post('functions.php',{"id":post.id,"title":post.title,"description":post.description,"category":post.category,"status":post.status}).success(function(data) { 
      if (data == true) { 
       getPosts(); 
      } 
     }); 
    } 
    $scope.deletePost = function(post) { 
     if (confirm("Are you sure you want to delete this post?")) { 
      $http.post('functions.php',{"id":post.id}).success(function(data) { 
       if (data == true) { 
        getPosts(); 
       } 
      }); 
     } 
    } 
}]); 

İşte benim PHP komut dosyası olmasıdır. Aynı PHP dosyasına her şeyi tutmak istiyorsanız

<?php 
    $connect = mysqli_connect('localhost', 'root', '', 'cms1'); 

    function getPosts() { 
     global $connect; 
     $query = "SELECT * FROM posts"; 
     $result = mysqli_query($connect, $query); 
     $posts = array(); 
     while ($row = mysqli_fetch_assoc($result)) { 
      $posts[] = $row; 
     } 
     echo $json_info = json_encode($posts); 
    } 

    function updatePost() { 
     global $connect; 
     $data = json_decode(file_get_connecttents("php://input")); 

     $id = mysqli_real_escape_string($connect, $data->id); 
     $title = mysqli_real_escape_string($connect, $data->title); 
     $description = mysqli_real_escape_string($connect, $data->description); 
     $image = mysqli_real_escape_string($connect, $data->image); 
     $category = mysqli_real_escape_string($connect, $data->category); 
     $status = mysqli_real_escape_string($connect, $data->status); 

     $query = "UPDATE posts SET title='$title',description='$description',image='$image',category='$category',status='$status' WHERE id=$id"; 
     mysqli_query($connect, $query); 
     echo true; 
    } 

    function deletePost() { 
     global $connect; 
     $data = json_decode(file_get_contents("php://input")); 
     $query = "DELETE FROM posts WHERE id=$data->id"; 
     mysqli_query($connect, $query); 
     echo true; 
    } 

?> 

cevap

0

Sen fonksiyonları kırmaya GET değişkenleri kullanabilirsiniz. senin angularjs içinde app.js dosyayı başvurulan

URL'ler sırasıyla functions.php?action=getPosts, functions.php?action=updatePost ve functions.php?action=deletePost için değiştirilmesi gerekecektir.

<?php 
    $connect = mysqli_connect('localhost', 'root', '', 'cms1'); 

    if ($_GET['action'] === 'getPosts') 
    { 
     global $connect; 
     $query = "SELECT * FROM posts"; 
     $result = mysqli_query($connect, $query); 
     $posts = array(); 
     while ($row = mysqli_fetch_assoc($result)) { 
      $posts[] = $row; 
     } 
     echo $json_info = json_encode($posts); 
    } 

    else if ($_GET['action'] === 'updatePost') 
    { 
     global $connect; 
     $data = json_decode(file_get_connecttents("php://input")); 

     $id = mysqli_real_escape_string($connect, $data->id); 
     $title = mysqli_real_escape_string($connect, $data->title); 
     $description = mysqli_real_escape_string($connect, $data->description); 
     $image = mysqli_real_escape_string($connect, $data->image); 
     $category = mysqli_real_escape_string($connect, $data->category); 
     $status = mysqli_real_escape_string($connect, $data->status); 

     $query = "UPDATE posts SET title='$title',description='$description',image='$image',category='$category',status='$status' WHERE id=$id"; 
     mysqli_query($connect, $query); 
     echo true; 
    } 

    else if ($_GET['action'] === 'deletePost') 
    { 
     global $connect; 
     $data = json_decode(file_get_contents("php://input")); 
     $query = "DELETE FROM posts WHERE id=$data->id"; 
     mysqli_query($connect, $query); 
     echo true; 
    } 
?> 
+0

ben denedim ama bu mesajları göstermez: Daha önce bahsedilen URL'lerindeki action GET değişkeni kullanırsanız

, PHP bu gibi görünmelidir? – sidlack

İlgili konular