Issue
I have an error where it will not send the id into the page with $_GET['id'] (#/post?id=1)
I get the following error:
Notice: Undefined index: id in D:\xampp\htdocs\ls\includes\post.php on line 3
I have a blog page. List blogs posts works fine.
The issue is when I try to send the id to my posts page:
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 (Called in init.php with 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);
}
?>
Solution
So what you need to do is make an $http
request or ajax request when communicating with server side scripts like PHP.
Angular is client side and will NOT parse PHP files. You can use $_GET
if you like and make a request like
$http.get('http://some.url?id=someId')
or I recommend using $_POST
var params = {id:someId};
$http.post('http://some.url',params);
That is how to make an HTTP request on the top level. You need to know when PHP returns something so let's listen for that.
var params = {id:someId};
$http.post('http://some.url',params).then(function(response) {
console.log(response);
});
You will see in your console what your php script has returned and at that point you can do what you want with the data in your angular app. See this codepen for an example:
http://codepen.io/anon/pen/xRYVBB?editors=1010
Since the PHP isn't on codepen and it is on my server, here is what it looks like.
ang-ajax-demo.php
<?php
if (isset($_GET['id'])) {
echo "Blog data for id: " . $_GET['id'];
} else {
echo "no id";
}
I HIGHLY recommend you rethink how you've done your app. Get rid of all the php files and use html files as our templates. You want your client side and server side files decoupled. Your angular app will make HTTP (ajax) requests to your PHP files.
Answered By - Ronnie
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.