使用 $_GET['id'] 从路线获取 ID 的问题(Angular 站点)

Issue with using $_GET['id'] to get ID from route (Angular Site)

我有一个错误,它不会将 id 发送到带有 $_GET['id'] 的页面 (#/post?id=1)

我收到以下错误:

Notice: Undefined index: id in D:\xampp\htdocs\ls\includes\post.php on line 3

参见GitHub:https://github.com/liamstewart23/LSPortfolio

我有一个博客页面。列出博客 posts 工作正常。

问题是当我尝试将 ID 发送到我的 post 页面时:

Blog.php

<?php
require_once("../admin/phpscripts/init.php");
$tbl="tbl_blog";
$getPosts = getAll($tbl);
//echo $getPosts;
?>
<section id="blog">
<h2 class="hidden">Blog Posts</h2>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-10 col-md-offset-1"><h2>Blog</h2></div>
<div class="col-xs-12 col-sm-12 col-md-10 col-md-offset-1"><h3>My thoughts, beliefs and complaints.</h3></div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-sm-offset-0 col-md-8 col-md-offset-2" id="blog-posts">
<?php
if(!is_string($getPosts)){
while($row = mysqli_fetch_array($getPosts)){
        echo "<h1>{$row['b_title']}</h1>";
        echo "<span>Posted: {$row['b_date']} by {$row['b_author']}</span><br><br>";
        echo "<a href=\"#/post?id={$row['b_id']}\">Read Post...</a><br><br>";
        }
        } else {
        //echo "nope...";
        }
        ?>
    </div>
</div>

Post.php

<?php
require_once("../admin/phpscripts/init.php");
echo $_GET["id"];   
echo "hello";
?>

Read.php(在 init.php 中用 require_once 调用)

<?php
function getAll($tbl){
    require_once("config.php");
    $queryAll = "SELECT * FROM {$tbl}";
    //echo $queryAll;
    $runAll = mysqli_query($link, $queryAll);
    if($runAll) {
        return $runAll;
    } else {
        $error = "There was an error accessing this information. Shoot me an email at me@lstew.com";
        return $error;
    }

    mysqli_close($link); //want to make sure that it is terminated, do not want anything accessible
} 


function getPost($id, $tbl, $col) {
    require_once("config.php");
    $querySingle = "SELECT * FROM {$tbl} WHERE {$col}={$id}";
    //echo $querySingle;
    $runSingle = mysqli_query($link, $querySingle);
    if($runSingle) {
        return $runSingle;
    }
    else {
        $error = "This is not the movie you are looking for...";
        return $error;
    }
    mysqli_close($link);
}
?>

所以您需要做的是在与 PHP.

等服务器端脚本通信时发出 $http 请求或 ajax 请求

Angular 是客户端,不会解析 PHP 文件。如果您愿意,可以使用 $_GET 并提出

之类的请求

$http.get('http://some.url?id=someId') 或者我建议使用 $_POST

var params = {id:someId};
$http.post('http://some.url',params);

这就是如何在顶层发出 HTTP 请求。你需要知道什么时候 PHP returns 所以让我们听听。

var params = {id:someId};
$http.post('http://some.url',params).then(function(response) {
  console.log(response);
});

您将在控制台中看到 php 脚本返回的内容,此时您可以对 angular 应用程序中的数据执行任何操作。有关示例,请参见此代码笔:

http://codepen.io/anon/pen/xRYVBB?editors=1010

由于 PHP 不在 codepen 上,而是在我的服务器上,下面是它的样子。

ang-ajax-demo.php

<?php
if (isset($_GET['id'])) {
  echo "Blog data for id: " . $_GET['id'];
} else {
  echo "no id";
}

我强烈建议您重新考虑您是如何完成您的应用程序的。删除所有 php 文件并使用 html 文件作为我们的模板。您希望您的客户端和服务器端文件分离。您的 angular 应用程序将向您的 PHP 文件发出 HTTP (ajax) 请求。