Hello Pals...
Help with this code...I am unable to hold Angularjs tag with Php variables

$vid = "{{task.vid}}"; //this where the Errors comes in :(
$s = $db->query("SELECT * FROM reply WHERE post_id_fk = '$vid'") or die($db->error.LINE);

Please how can i go about this? Thank you!

Dani AI

Generated

Brief diagnosis: PHP runs on the server before the browser runs AngularJS. Any Angular interpolation (the client-side curly-brace tokens) will not be evaluated when PHP builds and executes a SQL query, so embedding those tokens inside server-side code will fail — which explains why saw no result and why ’s suggestion of putting interpolation into the PHP query didn’t work.

Recommended pattern (safe and reliable): expose a small PHP endpoint that accepts the post id, then have Angular call that endpoint (AJAX). The server reads the incoming value, validates it, uses a prepared statement, and returns JSON. Example AngularJS call:

// from an AngularJS controller/service
$http.get('get_replies.php', { params: { vid: task.vid } })
  .then(function(response){
    $scope.replies = response.data;
  })
  .catch(function(err){
    console.error('fetch error', err);
  });

Example PHP endpoint (use PDO or mysqli prepared statements; cast/validate IDs and return JSON):

<?php
header('Content-Type: application/json; charset=utf-8');
$vid = isset($_GET['vid']) ? (int) $_GET['vid'] : 0;

$pdo = new PDO('mysql:host=localhost;dbname=DBNAME;charset=utf8mb4','USER','PASS',[
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

$stmt = $pdo->prepare('SELECT * FROM reply WHERE post_id_fk = :vid');
$stmt->execute([':vid' => $vid]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo json_encode($rows);

Troubleshooting & security notes: check the browser Network tab to confirm the request and response; set the response header to JSON; use prepared statements (never interpolate raw input into SQL); validate/cast IDs (e.g., (int)); enable proper error logging rather than exposing DB errors to users; handle CORS if the endpoint is on a different origin. If the value truly originates on the server at page render time, inject it into the page with PHP (json_encode into a JS variable) instead of expecting client interpolation to reach the server.

Recommended Answers

All 2 Replies

Hello Kindo,

Instead of assigning angularjs variable to php variable you can call direct in query string.

try this:
$s = $db->query("SELECT * FROM reply WHERE post_id_fk = '{{task.vid}}'") or die($db->error.LINE);

This should work with angularJS.

Thanks ...this doesn't work either

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.