Administration panel for the client of a reservation website in which the client can write and give his opinion about a reservation that has taken place.
There is a section to be able to evaluate the stay (selecting stars from 0-5) in terms of quality. If we do not mark anything in the value of the stars, the value is automatically 0.
Why if I do not mark any stars does it give me the following error?
Notice: Undefined index: stars in C:\xampp\htdocs\daw\menu_cliente.php on line 20
It tries to get the value of the stars, but since it is 0 , not selecting any, it does not find that value in$_POST["estrellas"];
1) How would this error be corrected?**
2) And also, the opinion can only be sent if the textarea is not empty.**
Code:
<?php
require_once "Clases/BD.php";
require_once "Clases/Opiniones.php";
//Iniciar una nueva sesión o reanudar la existente.
session_start();
//Si existe la sesión "cliente"..., la guardamos en una variable.
if (isset($_SESSION['cliente'])){
$cliente = $_SESSION['cliente'];
}
//Si pulsamos el botón "Añadir opinion"...
if(isset($_POST["add_opinion"])){
$idemail = $cliente;
$idcabana = $_POST["a_anadir"];
$opinion = $_POST["texto_opinion"];
$fecha = new DateTime();
$fecha_hoy = $fecha->format ('Y/m/d');
$valoracion = $_POST["estrellas"]; //Al no seleccionar nada, no coge el 0...
BD::insertarOpinionPorReserva($idemail, $idcabana, $opinion, $fecha_hoy, $valoracion);
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Panel del cliente</title>
</head>
<body>
<div id="sesion_cliente">
<?php
//Si existe la sesión "cliente"...
if(isset($_SESSION['cliente'])){
echo "<p class='negrita'>Bienvenido ".$cliente." ";
echo "<a href='reservar.php?salir=1'>Salir</a></p>";
//Si existe y hemos pulsado el link "Salir"...
if(isset($_REQUEST["salir"])){
//Borramos o destruimos la sesión "cliente".
unset($_SESSION["cliente"]);
}
}
?>
</div>
<div id="menu_cliente">
<ul id="menu_horizontal">
<li><a href="menu_cliente.php?nueva=1">NUEVA OPINIÓN</a></li>
<li><a href="menu_cliente.php?ver=1">VER OPINIONES</a></li>
<li><a href="menu_cliente.php?eliminar=1">ELIMINAR OPINIÓN</a></li>
<li><a href="menu_cliente.php?datos_cliente=1">DATOS PERSONALES</a></li>
</ul>
<div id="cuerpo_body">
<?php
//Si pulsamos el link "Nueva opinión"...
if(isset($_GET["nueva"])){
//Llamamos al método "obtenerReservasPorCliente" y le pasamos el parámetro del email del cliente.
$reservas = BD::obtenerReservasPorCliente($cliente);
echo "<form action='menu_cliente.php?nueva=1' name='form_add_opinion' id='form_add_opinion' method='POST'>";
$selected = true;
foreach ($reservas as $reserva){
$idreserva = $reserva["idreserva"];
$idcabana = $reserva["idcabana"];
$idemail = $reserva["idemail"];
$check_in = new DateTime($reserva["check_in"]);
$check_out = new DateTime($reserva["check_out"]);
$reserva_toString = "Reserva de la cabaña ".$idcabana. " | ".$idemail. " | ".$check_in->format('d/m/Y'). " | ".$check_out->format('d/m/Y')."<br/><br/>";
echo "<input type='radio' name='a_anadir' value='".$reserva["idcabana"]."' ".($selected?"checked":"").">".$reserva_toString."</input>";
$selected = false;
}
echo "<br/><p class='deja_opinion'>Deja tu opinión:</p><br/>";
echo "<textarea rows='4' cols='56' id='texto_opinion' name='texto_opinion'></textarea><br/>";
echo "<p class='valoracion'>";
echo "<input id='radio1' type='radio' name='estrellas' value='5'>";
echo "<label for='radio1'>★</label>";
echo "<input id='radio2' type='radio' name='estrellas' value='4'>";
echo "<label for='radio2'>★</label>";
echo "<input id='radio3' type='radio' name='estrellas' value='3'>";
echo "<label for='radio3'>★</label>";
echo "<input id='radio4' type='radio' name='estrellas' value='2'>";
echo "<label for='radio4'>★</label>";
echo "<input id='radio5' type='radio' name='estrellas' value='1'>";
echo "<label for='radio5'>★</label>";
echo "</p><br/>";
echo "<input type='submit' name='add_opinion' id='add_opinion' value='Añadir opinion'>";
echo "</form>";
}
?>
</div>
</div>
</body>
</html>
To validate the radius , you could simply do the validation using the syntax of a ternary operator.
To validate that a text is added to the opinion, I would make the same evaluation but also taking into account that the field with ! empty()