PHP - Dos dudas

 
Vista:
sin imagen de perfil

Dos dudas

Publicado por Victor (195 intervenciones) el 11/06/2013 12:29:22
Buenas gente.
Tengo muchas dudas,las pondré todas juntas y que ve vaya contestando quien pueda a lo que pueda :).
1.Como puedo recoger los valores de un formulario en un array y despues pasarselo a una funcion javaScript.
Yo lo que tengo puesto es esto:
Codigo formulario:
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
"        <table border="0" width="100%" cellspacing="0">
            <form action="nuevoIndex.php" name ="frmDatosUsuario"method="post">
                <?php
                $conectar=conectar_bd();
                $sql =$conectar->query("select * from usuario
                                        where nombre ='hola'");
                $datos=$sql->fetch_array();
                if($datos)
                {
                    echo "Datos haber hay";
                }else
                {
                    echo "No hay";
                }
                ?>
                <tr><th colspan="2" bgcolor="#cccccc">Datos personales</th></tr>
                <tr>
                    <td>Nombre</td>
                    <td><input type="text" name="campos[]" maxlength="40" size="40" value="<?php print($datos['nombre']); ?>" /></td>
                </tr>
                <tr>
                    <td>Apellidos</td>
                    <td><input type="text" name="campos[]" value="<?php if(!empty($datos['apellidos'])){print($datos['apellidos']);} ?>" maxlength="40" size="40"/></td>
                </tr>
                <tr>
                    <td>Direccion</td>
                    <td><input type="text" name="campos[]" value="<?php if(!empty($datos['direccion'])){print($datos['direccion']);} ?>" maxlength="40" size="40"/></td>
                </tr>
                <tr>
                    <td>Pais</td>
                    <td><input type="text" name="campos[]" value="<?php if(!empty($datos['pais'])){print($datos['pais']);} ?>" maxlength="20" size="40"/></td>
                </tr>
                <tr>
                    <td>Provincia</td>
                    <td><input type="text" name="campos[]" value="<?php if(!empty($datos['provincia'])){print($datos['provincia']);} ?>" maxlength="20" size="40"/></td>
                </tr>
                <tr>
                    <td>Codigo Postal</td>
                    <td><input type="text" name="campos[]" value="<?php if(!empty($datos['cp'])){print($datos['cp']);} ?>" maxlength="10" size="40"/></td>
                </tr>
                <tr>
                    <td>Ciudad</td>
                    <td><input type="text" disabled="campos[]" name="ciudad" maxlength="20" size="40" value="<?php if(!empty($datos['ciudad'])){print($datos['ciudad']);} ?>" /></td>
                </tr>
                <tr>
                    <td>
                        <input type="submit" value="Modificar" onclick="habilita('campos',this.frmDatosUsuario)">
                    </td>
                </tr>
            </form>"

-Codigo javaScript que recoge eso valores:
1
2
3
4
5
6
7
8
9
10
11
" <script type="text/javascript" charset="">
        habilita(campos,frmDatosUsuario)
        {
        $arrayCampos = document.frmDatosUsuario.getElementsByName('campos[]');
            alert('hola antes');
            for(i=0;i<$arrayCampos.length;i++)
         {
             alert("Hola");
         }
        }
    </script>"


2.Por otro lado,es un poco mas de php y javaScript tengo un formulario en el cual hay campos que inicialmente estan invisible y quiero que si se cumple una condicion se pongan visibles.
Codigo 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"        <table class="tabla">
            <tr>
                <td>
                    <table>
                        <tr>
                            <td>
                                <form name ="frmRegistro" id="frmRegistro" method="post" action="registroUsuarios.php" style="1090px;">
                                    <tr>
                                        <th>
                                            Nombre de usuario<label style="color: red">*</label>
                                        </th>
                                        <td>
                                            <input type="text" name="nombreReg" size="30" maxlength="30">
                                        </td>
                                        <td>
                                            <label style="visibility: hidden;" id="1" name="nObligatorio">Campo obligatorio</label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>
                                            Correo electronico<label style="color: red">*</label>
                                        </th>
                                        <td>
                                            <input type="text" name="mailReg" size="30" maxlength="50">
                                        </td>
                                        <td>
                                            <label style="visibility: hidden;" id="2" name="mailObligatorio">Campo obligatorio</label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th align="right">
                                            Password<label style="color: red">*</label>
                                        </th>
                                        <td>
                                            <input type="password"  name="passReg" size="30" maxlength="30">
                                        </td>
                                        <td>
                                            <label style="visibility: hidden;" id="3" name="passObligatorio">Campo obligatorio</label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th align="right">
                                            Repetir password<label style="color: red">*</label>
                                        </th>
                                        <td>
                                            <input type="password" name="pass2Reg" size="30" maxlength="30" style="">
                                        </td>
                                        <td>
                                            <label style="visibility: hidden;" name="pass2Obligatorio">Campo obligatorio</label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td  align="right">
                                            <input type="submit" value="Registro" onclick="validarCampos('nombreReg;nomObligatorio;mailReg;mailObligatorio;passReg;passObligatorio;pass2Reg;pass2Obligatorio',this.frmRegistro)">
                                        </td>
                                    </tr>
                                </form>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>"


Codigo JavaScript,la cosa es que si entra dentro de la condicion del if que no cambia la visibilidad de las etiquetasque yo quiero:
1
2
3
4
5
6
7
8
9
10
11
12
"function validarCampos(campos,formulario)
        {
            var arrayCampos = campos.split(";");

            for (i=0;i<arrayCampos.length;i++)
            {
                    if((formulario[arrayCampos[i]]).value=="")
                    {
                        formulario[arrayCampos[i+1]].value.style.visibility='visible';
                    }
                }
        }"

Respecto al "i+1" es por que la etiqueta que quiero poner visible está justo despues del input al cual le pregunto si tiene o no contenido.

Bueno hay mas pero con eso me conformo.
Si alguno quiere y tiene tiempo de contestarle a un pozo de dudas que deje su correo please.

Gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder