How to send a json_encode array from PHP to JS by parameter

-2

Hello to all the developers. I have a query ... EHH, I'm doing a practice of a CRUD .. and I have the functionality to add and remove completed .. now I need to place the user's data in a specific modal .. I'm trying to place the return of query in a json_encode and pass said array by parameter in a function to be received in JS. but when invoking that function it gives me an error of Uncaught SyntaxError: Unexpected end of input. Could someone help me with this error please ..

you can see that the data is being passed by parameter in the function. but when doing lick .. give that error. This is the code where the data in the database is loaded:

require_once('muestra.php');
try {
    $db = new Repository();
    $select = $db->Query('SELECT * FROM laptop ORDER BY id ASC');
    foreach ($select as $key) {     
    $datos = json_encode($key);
      echo '<tr>';
      echo '<td>'.'<center>'.$key['id'].'</center>';
      echo '<td>'.$key['marca'].'</center>';
      echo '<td>'.'<center>'.$key['pulgadas'].'</center>';
      echo '<td>'.$key['especificaciones'].'</center>';
      echo '<td>'.$key['precio'].'</center>';
      echo '<td><center><i style="cursor:pointer;" class="fa fa-trash" onclick="delet('.$key['id'].');"></i>'.'&nbsp'.'&nbsp'.'&nbsp'.'&nbsp'.'<i style="cursor:pointer;" class="fa fa-refresh" onclick="addform('.$datos.');" data-toggle="modal" data-target="#myModal"></i></center></td>';   
      echo '<tr/>';
    }
} catch (PDOException $e) {
    echo "Se ha producido un error al intentar mostrar los datos". $e->getMessage();
}

Here the file shows.php:

class Repository
{   
function getConnection()
{
  $username = 'root';
  $password = '1RizpPfTeBsqEbku';
  $dsn = 'mysql:dbname=tecno;host=127.0.0.1';
  try {
    $connection = new PDO($dsn,$username,$password);
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $connection;
  }
  catch (PDOException $e) {
  echo "Se he producido un Error al intentar conectar al Servidor MySql: ". $e->getMessage();
  }
  function Query($sql)
  {
  try {
  $connection = $this->getConnection();
  $query = $connection->prepare($sql);
  $query->execute();
  return $query;
  } catch (PDOException $e) {
  echo "Se ha producido un error en la Consulta". $e->getMessage(); 
    }
   }        
  }
    
asked by jose abraham lainez juarez 29.10.2017 в 17:25
source

1 answer

1

Php sends some headers with each data output from the server, if that output is composed of multiple echo with html and then an echo of a json, automatically html headers and the variable in javascript containing that output will only take as text its content.

Taking your code as an example

$ajax.get(url, function(response){ console.log(response); });

I would only show the html on the console.

So what do we do? It would be enough to define two functions:

  • One to return html generated after the query that will be included in a div / table / element of your page thanks to jQuery with innerHTML for example.
  • Another to return the list of the query as JSON.

The problem is that you would have to make two calls, double work, no?

And if you return only the JSON and generate the text of the template with templates strings? function obtenerDatos() { var misDatos; $ajax.get('http://localhost/bla/blabla', function (datosEnJson){ var plantillaFinal = ''; $.forEach(datosEnJson, function(dato){ plantillaFinal+= ' <tr> <td>${dato.propiedad}</td> </tr> '; }); $('table').html(templateFinal); misDatos = datosEnJson; }) }

    
answered by 29.10.2017 в 19:22