Error in undefined variable PHP

1

What a good day, I have the following code in PHP:

    <div class="wrap">
        <form id="ContactForm" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">

                <label>Asunto:</label>
                <input type="text" class="form-control" id="asunto" name="asunto" placeholder="Asunto:" value="<?php if(!$enviado && isset($asunto)) echo $asunto ?>">

                <label>E-mail:</label>
                <input type="text" class="form-control" id="email" name="email" placeholder="E-mail:" value="<?php if(!$enviado && isset($email)) echo $email ?>">

                <label>Mensaje:</label>
                <textarea name="mensaje" class="form_control" id="mensaje" placeholder="Escribe aqui tu mensaje"></textarea>
  <?php if(!empty($errores)): ?>
  <div class="alert error">
    <?php echo $errores; ?>
  </div>  
<?php elseif ($enviado): ?>
  <div class="alert exito">
    <p>Enviado Correctamente</p>
  </div>  
<?php endif ?>
            <input type="submit" name="submit" class="btn btn_primary" value="Enviar Correo"></input>
        </form>

In the part of the programming I have the following:

    <?php 

$errores='';
$enviado='';
$mensaje_enviar='';

if (isset($_POST['submit'])) {
    $asunto=$_POST['asunto'];
    $email=$_POST['email'];
    $mensaje=$_POST['mensaje'];

    if (!empty($asunto)) {
        $asunto=trim($asunto);
        $asunto=filter_var($asunto, FILTER_SANITIZE_STRING);
    }else{
        $errores='Por favor ingresa un nombre <br />';
    }
    if (!empty($email)) {
        $email=filter_var($email, FILTER_SANITIZE_EMAIL);
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            $errores .='Por favor ingresa un correo valido <br />';
        }
    }else{
        $errores .='Por favor ingresa un correo <br />';
    }

    if (!empty($mensaje)) {
        $mensaje=htmlspecialchars($mensaje);
        $mensaje=trim($mensaje);
        $mensaje=stripcslashes($mensaje);
    }else{
        $errores .='Por favor ingresa el mensaje <br />';
    }

    if (!$errores) {
        $destinatario='[email protected]';
        $asunto='Correo enviado desde';
        $mensaje_enviar="Asunto:" .$asunto;
        $mensaje_enviar.="E-mail:" .$email;
        $mensaje_enviar.="Mensaje:" . $mensaje;

        mail($destinatario, $asunto, $mensaje_enviar);
        $enviado=true;

    }
}

require 'Contacto.view.php';

 ?>

And it throws me the following error that I will attach in an image:

Could you help me with this code? The variable where the error marks me is already defined.

    
asked by Guillermo Ricardo Spindola Bri 08.04.2016 в 19:02
source

4 answers

2

Try it will work for you yourself you will see the error

This is the index.php now try it friend. Greetings !!!

<?php 

$errores='';
$enviado='';
$mensaje_enviar='';

if (isset($_POST['submit'])) {
    $asunto=$_POST['asunto'];
    $email=$_POST['email'];
    $mensaje=$_POST['mensaje'];

    if (!empty($asunto)) {
        $asunto=trim($asunto);
        $asunto=filter_var($asunto, FILTER_SANITIZE_STRING);
    }else{
        $errores='Por favor ingresa un nombre <br />';
    }
    if (!empty($email)) {
        $email=filter_var($email, FILTER_SANITIZE_EMAIL);
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            $errores .='Por favor ingresa un correo valido <br />';
        }
    }else{
        $errores .='Por favor ingresa un correo <br />';
    }

    if (!empty($mensaje)) {
        $mensaje=htmlspecialchars($mensaje);
        $mensaje=trim($mensaje);
        $mensaje=stripcslashes($mensaje);
    }else{
        $errores .='Por favor ingresa el mensaje <br />';
    }

    if (!$errores) {
        $destinatario='[email protected]';
        $asunto='Correo enviado desde';
        $mensaje_enviar="Asunto:" .$asunto;
        $mensaje_enviar.="E-mail:" .$email;
        $mensaje_enviar.="Mensaje:" . $mensaje;

        mail($destinatario, $asunto, $mensaje_enviar);
        $enviado=true;

    }
}

require 'contacto.view.php';

 ?>

This is the contact.view.php

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <div class="wrap">
        <form id="ContactForm" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">

                <label>Asunto:</label>
                <input type="text" class="form-control" id="asunto" name="asunto" placeholder="Asunto:" value="<?php if(!$enviado && isset($asunto)) echo $asunto ?>">

                <label>E-mail:</label>
                <input type="text" class="form-control" id="email" name="email" placeholder="E-mail:" value="<?php if(!$enviado && isset($email)) echo $email ?>">

                <label>Mensaje:</label>
                <textarea name="mensaje" class="form_control" id="mensaje" placeholder="Escribe aqui tu mensaje"></textarea>
  <?php if(!empty($errores)): ?>
  <div class="alert error">
    <?php echo $errores; ?>
  </div>  
<?php elseif ($enviado): ?>
  <div class="alert exito">
    <p>Enviado Correctamente</p>
  </div>  
<?php endif ?>
            <input type="submit" name="submit" class="btn btn_primary" value="Enviar Correo"></input>
        </form>
</body>
</html>
    
answered by 06.05.2016 в 22:39
1

the $ sent variable is not defined

    
answered by 08.04.2016 в 21:12
0

the $ sent variable, you have it started as a text string and then you are treating it as a Boolean capo, that can be one of the causes of the error. Try starting it so $ sent = false;

    
answered by 06.07.2016 в 16:34
0

First of all, a question. The PHP code is before or after HTML ?

If you are after HTML , that is why you are not capturing the $ sent variable. Being so:

The $ sent variable is not declared. The condition that you execute in the fields of the form is checking if $ sent === true or $ sent === false , but does not evaluate its existence, as does the function isset . Then, by not "finding" said variable, it launches a notice .

The solution that I would recommend, is that for the variables that you know are not yet declared or loaded, you should call them in the following way:

@ $ variable

And thus PHP will not throw any error in case of not finding the variable, then when the form is sent, the code is already read and the variables take their respective values and now the variable if taken into account:)

    
answered by 27.03.2017 в 07:04