Problem with header and foreach (headers already sent by) [duplicate]


I have a problem with a form in which I have several checkboxes. This is so they can choose various services.

The checkboxes have as value the fields id_del_servicio,id_de_descuento together. This is so that later in the PHP, with a foreach, it separates them and inserts them in their respective fields.

The problem is when I'm online: the header sends an error to the foreach that are empty. That is, services that were not selected and obviously does not direct me.

Warning: Cannot modify header information - headers already sent by (output started
foreach(($_POST['horno']) &horno  as $horno){

foreach($_POST['refrigerador'] as $ref){

foreach($_POST['ventanas'] as $ven){

foreach($_POST['lavado'] as $lav){

foreach($_POST['planchar'] as $pla){

foreach($_POST['hogars'] as $hog){

$cliente = (int)$user; 
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

$sql = "call sp_segundopaso_servicio('.$cliente.','.$horno[0].','.$horno[1].','.$ref[0].','.$ref[1].','.$ven[0].','.$ven[1].','.$lav[0].','.$lav[1].','.$pla[0].','.$pla[1].','.$hog[0].','.$hog[1].')";
if ($conn->query($sql) === TRUE) {

   header('Location: ../domicilio.php');

} else {


asked by Sistemas MKT 24.05.2016 в 00:50

2 answers


Before the header () there can be no output, (not even a warning) just the part of the most important message you did not put (output started ... , there tells you in which part of the script you are generating a previous output to the header

Sometimes to avoid these messages you just have to use an @ on the line, for example

$fp = @ fopen( '/etc/shadow', 'r' );
if ( $fp === FALSE )
  header( 'Location: /acaVanLosMalos.html' );
answered by 24.05.2016 в 01:44

Try passing an absolute path to the header, if this does not work you can always use this alternative:

    echo '<meta http-equiv="refresh" content="0; url=tu_pagina.php">';

I hope it will be helpful, greetings.

answered by 24.05.2016 в 00:56