En esta página se muestran una parte de las soluciones de los ejercicios Controles en formularios (1), concretamente el fragmento PHP, sin el principio ni el final de la página que se pueden dejar en fragmentos HTML.
Si lo prefiere, puede descargar unas posibles soluciones completas de estos ejercicios.
<?php
// Función de recogida de datos
function recoge($key, $type = "")
{
if (!is_string($key) && !is_int($key) || $key == "") {
trigger_error("Function recoge(): Argument #1 (\$key) must be a non-empty string or an integer", E_USER_ERROR);
} elseif ($type !== "" && $type !== []) {
trigger_error("Function recoge(): Argument #2 (\$type) is optional, but if provided, it must be an empty array or an empty string", E_USER_ERROR);
}
$tmp = $type;
if (isset($_REQUEST[$key])) {
if (!is_array($_REQUEST[$key]) && !is_array($type)) {
$tmp = trim(htmlspecialchars($_REQUEST[$key]));
} elseif (is_array($_REQUEST[$key]) && is_array($type)) {
$tmp = $_REQUEST[$key];
array_walk_recursive($tmp, function (&$value) {
$value = trim(htmlspecialchars($value));
});
}
}
return $tmp;
}
$nombre = recoge("nombre");
print " <p>Su nombre es <strong>$nombre</strong>.</p>\n";
?>
<?php
// Función de recogida de datos
function recoge($key, $type = "")
{
if (!is_string($key) && !is_int($key) || $key == "") {
trigger_error("Function recoge(): Argument #1 (\$key) must be a non-empty string or an integer", E_USER_ERROR);
} elseif ($type !== "" && $type !== []) {
trigger_error("Function recoge(): Argument #2 (\$type) is optional, but if provided, it must be an empty array or an empty string", E_USER_ERROR);
}
$tmp = $type;
if (isset($_REQUEST[$key])) {
if (!is_array($_REQUEST[$key]) && !is_array($type)) {
$tmp = trim(htmlspecialchars($_REQUEST[$key]));
} elseif (is_array($_REQUEST[$key]) && is_array($type)) {
$tmp = $_REQUEST[$key];
array_walk_recursive($tmp, function (&$value) {
$value = trim(htmlspecialchars($value));
});
}
}
return $tmp;
}
$ancho = recoge("ancho");
print " <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" \n"
. " width=\"{$ancho}px\" height=\"{$ancho}px\">\n";
print " <rect x=\"0\" y=\"0\" width=\"$ancho\" height=\"$ancho\" fill=\"black\" />\n";
print " </svg>\n";
?>
<?php
// Función de recogida de datos
function recoge($key, $type = "")
{
if (!is_string($key) && !is_int($key) || $key == "") {
trigger_error("Function recoge(): Argument #1 (\$key) must be a non-empty string or an integer", E_USER_ERROR);
} elseif ($type !== "" && $type !== []) {
trigger_error("Function recoge(): Argument #2 (\$type) is optional, but if provided, it must be an empty array or an empty string", E_USER_ERROR);
}
$tmp = $type;
if (isset($_REQUEST[$key])) {
if (!is_array($_REQUEST[$key]) && !is_array($type)) {
$tmp = trim(htmlspecialchars($_REQUEST[$key]));
} elseif (is_array($_REQUEST[$key]) && is_array($type)) {
$tmp = $_REQUEST[$key];
array_walk_recursive($tmp, function (&$value) {
$value = trim(htmlspecialchars($value));
});
}
}
return $tmp;
}
$fruta = recoge("fruta");
print " <p><img src=\"img/frutas/$fruta\" width=\"300\"></p>\n";
?>
Primera página (formulario):
<?php
$color = rand(0, 360);
print " <label><input type=\"checkbox\" name=\"fondo\" value=\"hwb($color 80% 0%)\"> Color del fondo de la página</label><br>\n";
print " <label><input type=\"checkbox\" name=\"letra\" value=\"hwb($color 0% 40%)\"> Color de la letra de la página</label>\n";
?>
Segunda página (resultado):
<?php
// Función de recogida de datos
function recoge($key, $type = "")
{
if (!is_string($key) && !is_int($key) || $key == "") {
trigger_error("Function recoge(): Argument #1 (\$key) must be a non-empty string or an integer", E_USER_ERROR);
} elseif ($type !== "" && $type !== []) {
trigger_error("Function recoge(): Argument #2 (\$type) is optional, but if provided, it must be an empty array or an empty string", E_USER_ERROR);
}
$tmp = $type;
if (isset($_REQUEST[$key])) {
if (!is_array($_REQUEST[$key]) && !is_array($type)) {
$tmp = trim(htmlspecialchars($_REQUEST[$key]));
} elseif (is_array($_REQUEST[$key]) && is_array($type)) {
$tmp = $_REQUEST[$key];
array_walk_recursive($tmp, function (&$value) {
$value = trim(htmlspecialchars($value));
});
}
}
return $tmp;
}
$fondo = recoge("fondo");
$letra = recoge("letra");
print " <style>body { background-color: $fondo; color: $letra; }</style>\n";
?>
<?php
// Función de recogida de datos
function recoge($key, $type = "")
{
if (!is_string($key) && !is_int($key) || $key == "") {
trigger_error("Function recoge(): Argument #1 (\$key) must be a non-empty string or an integer", E_USER_ERROR);
} elseif ($type !== "" && $type !== []) {
trigger_error("Function recoge(): Argument #2 (\$type) is optional, but if provided, it must be an empty array or an empty string", E_USER_ERROR);
}
$tmp = $type;
if (isset($_REQUEST[$key])) {
if (!is_array($_REQUEST[$key]) && !is_array($type)) {
$tmp = trim(htmlspecialchars($_REQUEST[$key]));
} elseif (is_array($_REQUEST[$key]) && is_array($type)) {
$tmp = $_REQUEST[$key];
array_walk_recursive($tmp, function (&$value) {
$value = trim(htmlspecialchars($value));
});
}
}
return $tmp;
}
$datos = recoge("datos", []);
print " <p>Su nombre completo es <strong>$datos[1] $datos[2]</strong>.</p>\n";
?>
<?php
// Función de recogida de datos
function recoge($key, $type = "")
{
if (!is_string($key) && !is_int($key) || $key == "") {
trigger_error("Function recoge(): Argument #1 (\$key) must be a non-empty string or an integer", E_USER_ERROR);
} elseif ($type !== "" && $type !== []) {
trigger_error("Function recoge(): Argument #2 (\$type) is optional, but if provided, it must be an empty array or an empty string", E_USER_ERROR);
}
$tmp = $type;
if (isset($_REQUEST[$key])) {
if (!is_array($_REQUEST[$key]) && !is_array($type)) {
$tmp = trim(htmlspecialchars($_REQUEST[$key]));
} elseif (is_array($_REQUEST[$key]) && is_array($type)) {
$tmp = $_REQUEST[$key];
array_walk_recursive($tmp, function (&$value) {
$value = trim(htmlspecialchars($value));
});
}
}
return $tmp;
}
$info = recoge("info", []);
print " <p style=\"font-size: {$info[2]}px\">$info[1]</p>\n";
?>
Primera página (formulario):
<?php
// Función de recogida de datos
function recoge($key, $type = "")
{
if (!is_string($key) && !is_int($key) || $key == "") {
trigger_error("Function recoge(): Argument #1 (\$key) must be a non-empty string or an integer", E_USER_ERROR);
} elseif ($type !== "" && $type !== []) {
trigger_error("Function recoge(): Argument #2 (\$type) is optional, but if provided, it must be an empty array or an empty string", E_USER_ERROR);
}
$tmp = $type;
if (isset($_REQUEST[$key])) {
if (!is_array($_REQUEST[$key]) && !is_array($type)) {
$tmp = trim(htmlspecialchars($_REQUEST[$key]));
} elseif (is_array($_REQUEST[$key]) && is_array($type)) {
$tmp = $_REQUEST[$key];
array_walk_recursive($tmp, function (&$value) {
$value = trim(htmlspecialchars($value));
});
}
}
return $tmp;
}
$info = recoge("info", []);
$tipo = recoge("tipo");
print " <p style=\"font-family: $tipo; font-size: {$info[2]}px; color: $info[3]\">$info[1]</p>\n";
?>