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;
}
$ancho = recoge("ancho");
print " <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" \n"
. " width=\"" . $ancho + 8 . "px\" height=\"" . $ancho + 8 . "px\">\n";
print " <rect fill=\"white\" stroke=\"black\" stroke-width=\"4\" "
. "x=\"2\" y=\"2\" width=\"" . $ancho + 4 . "\" height=\"" . $ancho + 4 . "\" />\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>Su fruta preferida es $fruta.</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;
}
$fruta = recoge("fruta");
print " <p><img src=\"img/frutas/$fruta\" width=\"300\"></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;
}
$fondo = recoge("fondo");
$letra = recoge("letra");
if ($fondo != "" && $letra != "") {
print " <p>Usted quiere cambiar: $fondo y $letra</p>\n";
} elseif ($fondo != "") {
print " <p>Usted quiere cambiar: $fondo</p>\n";
} elseif ($letra != "") {
print " <p>Usted quiere cambiar: $letra</p>\n";
} else {
print " <p>Usted no quiere cambiar nada</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;
}
$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;
}
$ancho = recoge("ancho");
$grosor = recoge("grosor");
print " <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" \n"
. " width=\"" . $ancho + 2 * $grosor . "px\" height=\"" . $ancho + 2 * $grosor . "px\">\n";
print " <rect fill=\"white\" stroke=\"black\" stroke-width=\"$grosor\" "
. "x=\"" . $grosor / 2 . "\" y=\"" . $grosor / 2 . "\" width=\"" . $ancho + $grosor . "\" height=\"" . $ancho + $grosor . "\" />\n";
print " </svg>\n";
?>