domingo, 19 de agosto de 2012

Cambiar propiedades CSS con PHP


En este ejemplo veremos como cambiar el ancho, el alto y el color de fondo de un div desde un formulario con PHP. 

Utilizaremos el método post para ir cambiando las propiedades width, heigth y background-color del CSS del div. 



Código de ejemplo ...

<?php

/* By http://php-estudios.blogspot.com */

if (isset($_POST["css"]))
{
$width=$_POST["width"];
$height=$_POST["height"];
$backcolor=$_POST["backcolor"];
}
else
{
$width="100px";
$height="100px";
$backcolor="red";
}

?>

<style>
#caja
{
width: <?php echo $width; ?>; 
height: <?php echo $height;?>; 
background-color: <?php echo $backcolor; ?>;
}
</style>

<!DOCTYPE HTML>
<html>
<head>
<title>Cambiar propiedades CSS con PHP</title>
</head>
<body>
<center>
<h1>Cambiar propiedades CSS con PHP</h1>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<table>
<tr><td>Ancho:</td> <td><input type="text" name="width" value="<?php echo $width; ?>"></td></tr>
<tr><td>Alto:</td> <td><input type="text" name="height" value="<?php echo $height; ?>"></td></tr>
<tr><td>Color fondo:</td> <td><input type="text" name="backcolor" value="<?php echo $backcolor; ?>"></td></tr>
<tr><td><input type="hidden" name="css"></td><td><input type="submit" value="Enviar"></td></tr>
</table>
</form>
<div id="caja"></div>
</center>
</body>
</html>




No hay comentarios: