Problem with php in header with form

1

I wanted to know if someone could give me a hand with this form that basically when you do not enter the required fields it refreshes in the same html. The subject that I always get this error

  

Warning: Can not modify header information - headers already sent by   (output started at /Applications/MAMP/htdocs/redirigitalmismo.php:6)   in /Applications/MAMP/htdocs/redirigitalmismo.php on line 16

and I do not know how to solve it. It's supposedly because I'm sending information before the header. Thank you very much

    <?php
  ob_start();
?>

<?php
$errores = array();
if($_POST){
  if(!trim($_POST['nombre'])) {
  $errores['nombre'] = 'Debe ingresar un nombre';
  }
  if(!trim($_POST['email'])) {
  $errores['email'] = 'Debes ingresar un mail';
  }
  if(!$errores) {
  header('Location: http://www.google.com');
  exit();
    }
};
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>
    <form  action=""  name="formulario" method="POST">
      <input type="text" name="nombre" id="nombre" maxlength="30" onfocus="fondoColor(this)" placeholder="Tu nombre">
      <?php if(isset($errores['nombre'])) { echo '<div>No pusiste Nombre</div>'; } ?>
      <br>
      <input type="email" name="email" value="" placeholder="Tu Mail">
      <?php if(isset($errores['email'])) { echo '<div>No ingresaste Mail</div>'; } ?>
      <br>
      <input type="radio" name="sexo" id="Hombre"value="Hombre"> Hombre
      <input type="radio" name="sexo" id="Mujer"value="Mujer"> Mujer
      <br>
      <input type="checkbox" name="terminos[]" id="terminos" value="uno" > Terminos y condiciones
      <br>
      <input type="checkbox" name="terminos[]" id="terminos"  value="dos"> Mas terminos y condiciones
      <br>
      <input type="submit" name="btn" value="Enviar" id="btn">
    </form>
  </body>
</html>
<?php
  ob_end_flush();
?>
    
asked by Infraganti 14.09.2016 в 17:21
source

4 answers

0

I already found the error! I did not realize because I was directly going to the file. The mamp automatically refreshes without stopping (and probe re install it) and then it does not let me do ANYTHING. Therefore surely I had problems in my php. Now I'm going to contact MAMP support. THANK YOU TO ALL FOR THE HELP

    
answered by 14.09.2016 / 20:51
source
0

Try to make use of this:

// antes de cualquier línea de código html o php:
<?php
  ob_start();
?>

// al final del documento:

<?php
  ob_end_flush();
?>
    
answered by 14.09.2016 в 17:35
0

It happens that at the beginning you are not conditioned, you are declaring that the value of name and email is equal to what is in the quotes:

<?php
$errores = [];
if($_POST){
if(!trim($_POST['nombre'])) {
$errores['nombre'] = 'Debe ingresar un nombre';
}
if(!trim($_POST['email'])) {
$errores['email'] = 'Debes ingresar un mail';
}
if(!$errores) {
header('Location: http://www.google.com');
exit();
}
};
?>

Add the error of the other file you mention.

On the other hand, the array must be declared with the syntax:

$errores = array();

The error is caused by the syntax of the header, declares the html before, since you are modifying it later and that is not functional

It would stay like this:

<!DOCTYPE html>
<?php
$errores = array();
if($_POST){
if(!trim($_POST['nombre'])) {
   $errores['nombre'] = 'Debe ingresar un nombre';
}
if(!trim($_POST['email'])) {
$errores['email'] = 'Debes ingresar un mail';
} 
if(!$errores) {
 header('Location: http://www.google.com');
exit();
}
};
?>

<html>
<head>
 <meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form  action=""  name="formulario" method="POST">
  <input type="text" name="nombre" id="nombre" maxlength="30"   onfocus="fondoColor(this)" placeholder="Tu nombre">
  <?php if(isset($errores['nombre'])) { echo '<div>No pusiste Nombre</div>';     } ?>
  <br>
  <input type="email" name="email" value="" placeholder="Tu Mail">
  <?php if(isset($errores['email'])) { echo '<div>No ingresaste Mail</div>'; } ?>
  <br>
  <input type="radio" name="sexo" id="Hombre"value="Hombre"> Hombre
  <input type="radio" name="sexo" id="Mujer"value="Mujer"> Mujer
  <br>
  <input type="checkbox" name="terminos[]" id="terminos" value="uno" > Terminos y condiciones
  <br>
  <input type="checkbox" name="terminos[]" id="terminos"  value="dos"> Mas terminos y condiciones
  <br>
  <input type="submit" name="btn" value="Enviar" id="btn">
</form>

I invite you to review this page: link

    
answered by 14.09.2016 в 17:24
-2

Try adding HTML first

<!DOCTYPE html>
<?php
$errores = [];
bla, bla, bla ...
    
answered by 14.09.2016 в 17:31