I have a list that generates a button for each existing record, it receives the ID
of the corresponding record and then redirects to another page if it is clicked.
This is your HTML Code:
List_Registries.html
<input type="text" name="consultado" id="consultado">
<table>
<tr>
<th>ID del registro</th>
</tr>
<?php
while ($datos = $resultados->fetch_assoc())
{
?>
$stmt = $conexion->prepare("SELECT id FROM registros");
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id);
$stmt->fetch();
$stmt->close();
?>
<tr>
<td><?php $id = $datos['id']; echo $id; ?></td>
<td><a onclick="enviame();" href="tablagrafica.php?id=<?=$id?>">Grafica</a></td>
</tr>
<?php
}
?>
If there is an error in PHP, it is because you try to copy it as little as possible.
The
consultado
field will be explained later.
The tablagrafica.php
file receives that id
of the href
command and displays the data perfectly.
The problem is that:
- If a user enters the URL
tablagrafica.php?id=12412
, it will show that graph and would like to avoid that.
So apply the following solution:
Place a hidden field and the value of this field is compared when trying to access the graphical table, if it is not the expected value it will not be able to show it.
This I did with this JS code called enviame()
:
function enviame(){
var agree=confirm("\u00BFDesea obtener esta grafica?.");
if (agree){
document.getElementById("consultado").value = 1;
return true ;
}
else
return false ;
}
Its function was to place a value in the text field called consultado
and when redirecting to the file tablagrafica.php
this POSTEARA
if there was the value showed it but NO , comparing it with 1.
tablagrafica.php:
$consultado = $_POST["consultado"];
if ($consultado== 1)
{
//Conexion y grafica.
$id=$_REQUEST["id"]; //Esto funciona bien.
}
else
{
echo ("<script>window.location = 'index.php';</script>");
}
What happened: It did not work.
I think it acts before the
HREF
than the JS code, because I always says that theconsultado
variable is not defined.
Reading I got that to get the value of consultado
I should do a submit
.
If I do it using javascript formulario.submit
, it will not get the value of ID
, since this value is generated for each button and when using REQUEST
you will expect a value for HEADER
.
If I try to get with POST
the ID
does not work, I only get the last ID
that was shown with the HTML
, if there are 30 records and I hit the button of the first one, I will get the id = 30
I conclude: I would like to redirect the HREF
of the record with the ID
and also make the POST
of the variable consultado
in order to have greater security when obtaining the graphic table.
Modification suggested by @Lixus:
Listing.html
<input type="text" name="consultado" id="consultado">
<table>
<tr>
<th>ID del registro</th>
</tr>
<?php
while ($datos = $resultados->fetch_assoc())
{
?>
$stmt = $conexion->prepare("SELECT id FROM registros");
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id);
$stmt->fetch();
$stmt->close();
?>
<tr>
<input type="text" name="id" value="<?php $id= $datos['id']; echo
$id; ?>" hidden>
<td><?php $id = $datos['id']; echo $id; ?></td>
<td><input type="submit" name="ver" id="ver" value="Ver"></td>
</tr>
<?php
}
?>
What happened: Clicking on any of the POST
takes the last ID
that exists
Modification suggested by @Robertos:
Listing.html
<form name="listado" id="listado" method="POST">
<table>
<tr>
<th>ID del registro</th>
</tr>
<?php
while ($datos = $resultados->fetch_assoc())
{
?>
$stmt = $conexion->prepare("SELECT id FROM registros");
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id);
$stmt->fetch();
$stmt->close();
?>
<tr>
<input type="text" name="id" value="" hidden>
<td><?php $id = $datos['id']; echo $id; ?></td>
<td><a href="javascript:enviame('tablagrafica.php', '<?=$id?>')">Ver</a></td>
<?php
}
?>
<input type="text" id="consultado" value="1";
Javascript
function enviame(laUrl, elId){
var elform =document.getElementById("listado");
var eldato=document.getElementById("id");
eldato.value=elId;
elform.action=laUrl;
elform.submit();
}
tablagrafica.php
What happened: Pressing the see button makes the redirection but tablagrafica.php
tells me that querying and id
are not defined
When doing elform alert and data, they return undefined and the id.
tablagrafica.php
$consultado = $_POST["consultado"];
$id = $_POST["id"];
if ($consultado== 1)
{
//Conexion y grafica.
$id=$_REQUEST["id"]; //Esto funciona bien.
}
else
{
echo ("<script>window.location = 'index.php';</script>");
}