AngularJS将URL param传递给PHP


angularjs pass url param to php

如何使用 angularjs 路由将 URL 参数传递给 php 变量?

这是routing_script.js:

var scotchApp = angular.module('HRModuleApp', ['ngRoute']);
scotchApp.config(function($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl : 'pages/home.html',
            controller  : 'mainController'
        })
        // route for the home page
        .when('/home', {
            templateUrl : 'pages/home.html',
            controller  : 'mainController'
        })
    // route for the contact page
        .when('/public_profile:user_id', {
            templateUrl : 'pages/public_profile.php',
            controller  : 'contactController'
        })
        // route for the contact page
        .when('/add_user', {
            templateUrl : 'pages/add_user.html',
            controller  : 'contactController'
        });
});

我还应该添加什么? 如何将id_user参数从 URL 转发到 PHP 变量...所以PHP可以执行一些sql...

我还读到过角度不适合这样的事情......但我需要它...我急需它...

谢谢!!!

以下是在 PHP 文件中需要该 URL 参数的变量$term:

<?php
$term = mysql_real_escape_string($_REQUEST['user_id']);

好的,如果你的问题是正确的,您希望"pages/public_profile.php"读取由 angular 传递的 GET 参数,以便它可以在渲染视图之前执行,也许这样的事情就可以了:

    .when('/public_profile/:user_id', {
        templateUrl: function(attrs){ 
            return 'pages/public_profile.php?user_id=' + attrs.user_id; },
        controller  : 'contactController'
    })

通过将"模板 URL"从字符串更改为将参数作为 get 参数添加到 url 的函数

在你的contactController

function contactcontroller(['$scope','$stateParams','$http'],function($scope,$stateParams,$http){
       //here you have your user_id
       var user_id = $stateParams.user_id;
      //now you can use $http to make a request to your server
       $http.post('your_url',{'usr_id':user_id})
           .success(function(response){
                //handle OK response
            })
           .error(function(response){
                //habndle error
            })
})

注意:您应该将所有$https调用移动到服务。

这是触发onclick事件的JavaScript...单击数据表中的行...我得到的链接看起来不错,但 php 脚本没有得到那个网址

    $(document).ready(function() {
    $(function(){
        $.ajax({
            url: 'http://localhost/hrmodel/public/pages/employees_datatables.php',
            data: {},
            dataType: 'json',
            success: function (data) {
            // Check if received data is indeed JSON Object and not a string
                if (data.substring) {
                    console.log('is string');
                } else{
                    console.log('is not string');
                }
            // Setup for individual column search - add a text input to each footer cell
                $('#employees tfoot th').each( function () {
                    var title = $('#employees thead th').eq( $(this).index() ).text();
                    $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
                } );
            // Initialize Datatable
                var table = $('#employees').DataTable( {
                   "bProcessing": true,
                   "bSearchable" : true,
                   "bSortable" : true,
                   "bFilter": true,
                   "bInfo": true,
                   "bPaginate" : true,
                   "data" : data,
                   "columns": [
                        { "data": "korisnik_id",
                            "visible": false,
                            "searchable": false
                        },
                        { "data": "ime" },
                        { "data": "prezime" },
                        { "data": "3" },
                        { "data": "naziv" }
                    ]
                } );
            // Apply the search for each column
                table.columns().eq( 0 ).each( function ( colIdx ) {
                   $( 'input', table.column( colIdx ).footer() ).on( 'keyup change', function () {
                       table
                           .column( colIdx )
                           .search( this.value )
                           .draw();
                   } );
                } );
           // Highlighting rows
                var lastIdx = null;
               $('#employees tbody').on( 'mouseover', 'td', function () {
                   var colIdx = table.cell(this).index().column;
                   if ( colIdx !== lastIdx ) {
                       $( table.cells().nodes() ).removeClass( 'highlight' );
                       $( table.column( colIdx ).nodes() ).addClass( 'highlight' );
                    }
                } )
                .on( 'mouseleave', function () {
                   $( table.cells().nodes() ).removeClass( 'highlight' );
                } );
            // Send user_id of selected row to PHP script
                $('#employees tbody').on( 'click', 'tr', function () {
                   var id = table.row( this ).data().korisnik_id;
                   $(this).addClass('chosenUser');
                   window.location = '#/public_profile.php?user_id=' + id;
/*
                   $.ajax({
                        type: 'POST',
                        url: 'http://localhost/hrmodel/public/pages/public_profile.php',
                        data: { user_id : id },
                        dataType: 'json',
                        success: function (data) {}
                    } );
*/
                } );
            } // End of ajax : success
        }); // End of .ajax request
    }); // End of function()
}); // End of document.ready()