pass php value to js giving error

-1
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}

table, td, th {
border: 1px solid black;
padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"banco_dados");
$sql="SELECT * FROM usuario WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>nome</th>
<th>snome</th>
<th>idade</th>
<th>cidade</th>
<th>estado</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['nome'] . "</td>";
echo "<td>" . $row['snome'] . "</td>";
echo "<td>" . $row['idade'] . "</td>";
echo "<td>" . $row['cidade'] . "</td>";
echo "<td>" . $row['estado'] . "</td>";
echo "</tr>";
$json[] = $row;
}
echo "</table>";
mysqli_close($con);

$teste = $row['nome'];

?>
 <script type="text/javascript">
   var dados = <?php echo json_encode($json)?>;
   alert(dados[2].nome);
  </script>

</body>
</html>

look at the last 8 lines returns me an alert plus no data in the variables so it returns this is that I need to work with this data in a js understand.

You know how I can pass these vlores to js?

    
asked by anonymous 25.04.2015 / 03:47

2 answers

1

Well, if I understood your question well, you need to get the data coming from the back end and use it in a js function, right?

One option is to generate a JSON with this data and manipulate it according to your needs. I'll give you an example of how your while loop would look in php:

$json = array();
while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['nome'] . "</td>";
  echo "<td>" . $row['snome'] . "</td>";
  echo "<td>" . $row['idade'] . "</td>";
  echo "<td>" . $row['cidade'] . "</td>";
  echo "<td>" . $row['estado'] . "</td>";
  echo "</tr>";
  $json[] = $row; //adiciona o row dentro do objeto json
}

After that, to make this object accessible in js, open a script tag and write:

var dados = <?php echo json_encode($json)?>;

So you can access any value using:

alert(dados[0].nome);

Replace 0 with the number of the row you want to use. The json object is independent of the table that was printed in HTML, so you do not have to worry if you need to change something in it.

    
25.04.2015 / 23:51
0

The problem is that you are setting $teste = $row['nome']; out of while , that is, the $row['nome'] variable no longer exists. Ideally, you set the variable outside the while (before), and within it, assign a value. This way:

$teste = "Erro ao pegar o valor"; // Cria uma variável com um valor inicial
while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['nome'] . "</td>";
  echo "<td>" . $row['snome'] . "</td>";
  echo "<td>" . $row['idade'] . "</td>";
  echo "<td>" . $row['cidade'] . "</td>";
  echo "<td>" . $row['estado'] . "</td>";
  echo "</tr>";
  $teste = $row['nome']; // Atribui um novo valor para a variável
}

This way, even if the query goes wrong, the $teste variable will exist, and it will automatically display a message saying that the query was not successful.

    
25.04.2015 / 03:55