JSON Search for specific page data

2

Hello! I'm trying to pull data from an API I made in PHP to send in JSON to another website. I was able to pull the result of an entire employee table, however now I want when I click on the employees to appear the other data related to it as Phone, address ... pulling by the id of it.

The API that links index.html

<?php

require_once 'bd2.php';
header('Access-Control-Allow-Origin:*');
$pdo = conectar();
$listar = $pdo->query("select * from funcionarios");
echo json_encode($listar->fetchAll(PDO::FETCH_OBJ));

 ?>

The javascript that does the JSON of index.html

$(document).ready(function(){
  var container = $("#teste2");
  var lista = container.find("#lista");
  var html = '';

  $.getJSON('http://teste.com/api-home.php', function(data){
    $.each(data, function(k, v){
      html += '<p>Nome: '+v.nome+'</p>';
      html += '<p>E-mail: '+v.email+'</p>';
      html += '<a href="perfil.html?id='+v.id+'">Perfil</a>'
    });
    lista.html(html);
  });
});

The profile API that connects to the .html profile

<?php
require_once 'bd2.php';
header('Access-Control-Allow-Origin:*');
$pdo = conectar();
$id = $_GET['id'];
$listar = $pdo->query("select * from funcionarios where id='$id'");
echo json_encode($listar->fetchAll(PDO::FETCH_OBJ));

 ?>

The javascript that makes the JSON profile.html

$(document).ready(function(){
  var container = $("#teste5");
  var lista = container.find("#lista2");
  var html = '';

  $.getJSON('http://teste.com/api-perfil.php', function(data){
    $.each(data, function(k, v){
      html += '<p>Telefone: '+v.telefone+'</p>';
      html += '<p>Endereço: '+v.endereco+'</p>';
    });
    lista.html(html);
  });
});
    
asked by anonymous 03.12.2017 / 17:08

1 answer

0

I think I need to pass the ID as a parameter in the javascript profile page.html.

In this part of the profile API you are doing a GET to get the id and look for the official with this id .

...
   $id = $_GET['id'];
   $listar = $pdo->query("select * from funcionarios where id='$id'");
...


However, in the profile page.html javascript you are not passing this id as a parameter so your API will not be able to query the database.

...
   $.getJSON('http://teste.com/api-perfil.php', function(data){
...


So when you click on an employee you need to get the ID and pass next to the profile API url:

...
   $.getJSON('http://teste.com/api-perfil.php?id='+ID, function(data){
...

In this way when the API code is to make the $ GET ['id'] it will get the id strong> and you will have to find the employee for your id .

    
04.12.2017 / 10:58