Clase para crear formularios – FormBox 0.1

Jueves, 21. Enero 2010

Jodiendo un rato con poo en php codee una simple clase

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
class form
{
    private $action;
    private $method;
    private $size;
    private $maxlength;
    private $id;
    private $src;
    private $elements;
    private $name;
    private $value;
    private $text;
    public function __construct($action, $method)
    {
        $this->action = $action;
        $this->method = $method;
    }
    public function openform()
    {
        echo "<form action=\"" . $this->action . "\" method=\"" . $this->method . "\"> \n";
    }
    public function field_text($id, $size, $maxlength)
    {
        $this->id = $id;
        $this->size = $size;
        $this->maxlength = $maxlength;
        echo "<input type=\"text\" id=\"" . $this->id . "\" size=\"" . $this->size . "\" maxlength=\"" .
            $this->maxlength . "\"> \n";
    }
    public function field_password($id, $size, $maxlength)
    {
        $this->id = $id;
        $this->size = $size;
        $this->maxlength = $maxlength;
        echo "<input type=\"password\" id=\"" . $this->id . "\" size=\"" . $this->size .
            "\" maxlength=\"" . $this->maxlength . "\"> \n";
    }
    public function select($elements = array(), $name)
    {
        $this->name = $name;
        $this->elements = $elements;
        echo "<select name=\"" . $this->name . "\"> \n";
        foreach ($this->elements as $single) {
            echo "<option name=\"" . $single . "\" value=\"" . $single . "\">" . $single .
                "</option> \n";
        }
        echo "</select>";
    }
    public function field_image($src)
    {
        $this->src = $src;
        echo "<input type=\"image\" src=\"" . $this->src . "\">";
    }
    public function field_button($name, $value)
    {
        $this->name = $name;
        $this->value = $value;
        echo "<input type=\"button\" name=\"" . $this->name . "\" value=\"" . $this->
            value . "\"> \n";
    }
    public function checkbox($name, $value, $text)
    {
        $this->name = $name;
        $this->value = $value;
        $this->text = $text;
        echo "<lebel><input type=\"checkbox\" name=\"" . $this->name . "\" value=\"" . $this->
            value . "\"> " . $this->text . "</label> \n";
    }
    public function closeform()
    {
        echo "</form> \n";
    }
}
?>

Validacion de formularios con PHP y JS

Miércoles, 5. Agosto 2009

Bueno aqui unos simples ejemplos ^^

PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style type="text/css">
#contenedor{
  text-align: center;
  }
#validar{
  border: 1px #C0C0C0 solid;
  }
#boton{
  border: 1px #C0C0C0 solid;
  }
</style>
</head>
<body>
<br />
<div id="contenedor">
<form action="" name="asd" method="POST">
<label>Campo a validar</label>
<br />
<input type="text" id="validar" name="validar" size="30" maxlength="30" />
<br />
<br />
<?php
  //Comprobamos que este vacio
  if (isset($_POST['validar'])) {
      $validar = htmlspecialchars(trim($_POST['validar']));
      //comprobamos que no este vacio al mandar el formulario
      if ($validar == '') {
          echo "<label>El campo " . $validar . " esta vacio</label>
<br />"
;
      }
  }
?>
<input type="submit" id="boton" value="Validar">
</form>
</div>
</body>
</html>

JS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   
    <head>
        <script type="text/javascript">
            //iniciamos la funcion para la validacion
            function validacion() {
                //obtenemos el valor del campo por DOM
                var validar = document.getElementById('validar');
                var nvalidar = validar.name;
                //comprobamos que no este vacion el campo
                if (validar.value == '') {
                    alert("El campo " + nvalidar + " esta vacion");
                } else {
                    //si no esta vacio el campo, mandamos formulario
                    document.forms.asd.submit();
                }
            }
        </script>
        <style type="text/css">
            #contenedor{ text-align: center; } #validar{ border: 1px #C0C0C0 solid;
            } #boton{ border: 1px #C0C0C0 solid; }
        </style>
    </head>
    <body>
        <br />
        <div id="contenedor">
            <form action="" name="asd" method="POST">
                <label>
                    Campo a validar
                </label>
                <br />
                <input type="text" id="validar" name="validar" size="30" maxlength="30"
                />
                <br />
                <br />
                <input type="button" id="boton" value="Validar" onclick="validacion()">
            </form>
        </div>
    </body>
</html>