I am trying to delete a line and that after deleting the line it redirects to the main page, but I cannot enter to redirect it.
If I set it like this, it stays inside the if where you see "it has not been redirected"
<form method="POST" action="">
<input type="hidden" name="borrar_linea_economia" value="<?php $l_economia['id_factura'] ?>">
<input type="submit" id="btn-basura" value="Eliminar">
</form>
<?php
if (isset($_POST['borrar_linea_economia'])){
RepositorioEconomia::borrar_economia_id(Conexion::obtener_conexion(),$l_economia['id_factura']);
if (headers_sent()) {
// las cabeceras ya se han enviado, no intentar añadir una nueva
echo "No se ha redirigido";
} else {
Redireccion::redirigir("http://localhost:8080/prueba/clientes/clientes.php");
}
If I set it like this I get the following error:
if (isset($_POST['borrar_linea_economia'])){
RepositorioEconomia::borrar_economia_id(Conexion::obtener_conexion(),$l_economia['id_factura']);
Redireccion::redirigir("http://localhost:8080/prueba/clientes/clientes.php");
}
Here the error:
In this other post I solved it with the first option but now I don't know why I can't and I can't find the solution.
You clearly haven't read the manual. the redirection in php is done through the headers, these are sent to the browser before the content of the page.
If there is something escaped before modifying the headers, they will not be written, therefore it will not work.
There should be nothing, not even a space before a call to header() where you write the headers.
that is why it is getting stuck because the id is after the html tags that you already send to the browser.
when you want to redirect and you have already sent headers or an http response to the client you should use javascript instead of php.
As you can see with this reference image, the headers are generated before the body, therefore you cannot modify them once you have escaped something to the html page
when this happens it is best to use a redirection via javascript. for instance: