Javascript 如何使用AngularJS使用$\u GET调用php脚本中的特定函数?
我使用AngularJS访问PHP脚本,但我只想调用一个特定的函数。在下面的app.js脚本中,通过单击按钮调用updatePost和deletePost函数。我试图使用get函数调用它们,但它不起作用 functions.phpJavascript 如何使用AngularJS使用$\u GET调用php脚本中的特定函数?,javascript,php,angularjs,function,http,Javascript,Php,Angularjs,Function,Http,我使用AngularJS访问PHP脚本,但我只想调用一个特定的函数。在下面的app.js脚本中,通过单击按钮调用updatePost和deletePost函数。我试图使用get函数调用它们,但它不起作用 functions.php <?php $connect = mysqli_connect('localhost', 'root', '', 'cms1'); if ($_GET['action'] === 'getPosts') { $query = "
<?php
$connect = mysqli_connect('localhost', 'root', '', 'cms1');
if ($_GET['action'] === 'getPosts') {
$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);
}
elseif ($_GET['action'] === 'updatePost') {
$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;
}
elseif ($_GET['action'] === 'deletePost') {
$data = json_decode(file_get_contents("php://input"));
$query = "DELETE FROM posts WHERE id=$data->id";
mysqli_query($connect, $query);
echo true;
}
?>
警告:使用
mysqli
时,应使用参数化查询,并将用户数据添加到查询中。不要使用手动转义和字符串插值或串联来完成此操作,因为这样会有创建严重错误的风险*谢谢你的提示,但是你知道我如何解决我的问题吗?我不懂PHP,但看起来你有一个打字错误:文件\u-get\u-connecttents
——我也很奇怪,你在$\u-get
中寻找POST
请求。我不确定这是否是正常的PHP。谢谢你指出错误,但我还是不能用angularjs调用if语句?
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?action=updatePost',{"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?action=deletePost',{"id":post.id}).success(function(data) {
if (data == true) {
getPosts();
}
});
}
}
}]);