Problem when collecting data from a php form. Very basic

0

I'm starting in php, and I have to do an exercise to process the data in a form. The exercise is simple, but I am jammed. I've been looking and looking for it for 4 days, but I can not find a solution.

It is an agenda. I have 2 input text to put name and number, and I have to send them to the server. I pick them up in two arrays. Now, I can not push them. I tried array_push, I tried everything.

I'm looking at a similar exercise from my teacher, I leave the code.

<body>
        <?php
            // Se comprueba que se ha enviado un número entre 1 y 49 si no es así la variable $numero toma el valor vacío
            if (isset($_POST['numero'])  && is_numeric($_POST['numero']) && $_POST['numero']>=0 && $_POST['numero']<=49)
                $numero=$_POST['numero'];
            else 
                $numero="";

            // Se crea el array para contener los distintos números que el usurio vaya guardando
            // Los valores se guardan en variables input de tipo hidden, cuyos nombre varían de "a0" a "a5"
            $quiniela=array(); //Creación del array

            // El código va recorriendo las variables hidenn transportadas y se introducen en quiniela
            $salir=false;
            $i=0;
            while (!$salir){
                if (isset($_POST["a".$i])){
                    $quiniela[$i]=$_POST["a".$i];
                    $i++;
                }else
                    $salir=true;
            }

            // comprobamos si el nuevo número que introduce el usuario esta en la lista, si no es así, se incluye
            $estaEnLaLista=false;
            if (count($quiniela)<6 && !empty($numero)){
                for($i=0;$i<count($quiniela) && !$estaEnLaLista; $i++)
                    if ($quiniela[$i]==$numero)
                        $estaEnLaLista=true;
                if (!$estaEnLaLista)
                    $quiniela[]=$numero;
            }
        ?>
        <header><h1>Números Lotería Primitiva</h1></header>
        <section>
            <div id="formulario">
                <!-- formulario que inserta el nuevo valor del usuario -->
                <form action="<?=$_SERVER['PHP_SELF']?>" method="post" id="formu">
                    <label for="numero">Número: </label> <input type="text" id="numero" name="numero" maxlength="2" size="2" /> <br />
                    <input type="submit" value="Enviar" /><br />

                    <!--
                        Cada vez que actualizamos la página creamos valores de tipo input hidden para transportar los valores escogidos por el usuario 
                    -->
                    <?php
                        for ($i=0; $i<count($quiniela); $i++)                   
                            print "<input type='hidden' name='a".$i."' value='".$quiniela[$i]."' />";
                    ?>
                </form>
            </div>
            <!-- Presentación de los valores escogidos por el usuario -->
            <div class="escogidos">
                    <?php
                        for ($i=0; $i<count($quiniela); $i++)
                            if ($i==0)
                                print $quiniela[$i];
                            else
                                print ", ".$quiniela[$i];
                    ?>
            </div>
        </section>
    </body>

That is the code of my professor, and what I do not understand, is this part.

<?php
    for ($i=0; $i<count($quiniela); $i++)                   
    print "<input type='hidden' name='a".$i."' value='".$quiniela[$i]."' />";
?>

I have explained 3-4 times, which is to send the data by hidden, which is mandatory, but I do not understand, and putting echo $ names, does not show anything, so I know, the vector is empty , and it is, because they do not reach the vector.

I'm lost, and I understand the code perfectly, I know why everything is done, but I get lost when sending the hidden data to the vectors.

I, I have several codes, I've already tried to do with a single vector, putting names in index and number in value, but it does not work out either. This is what I do, and it does not work out.

<?php 
        $nombres = [];
        $numeros = [];

        if (isset($_POST['nombre'])){
            $nombreAux = $_POST['nombre'];
        }else{
            $nombreAux = "";
        }
        if (isset($_POST['numero'])){
            $numeroAux = $_POST['numero'];
        }else{
            $numeroAux = "";
        }

        echo $nombreAux;
        echo $numeroAux;

        $salir=false;
        $i=0;
            while (!$salir){
                if (isset($_POST["nombre".$i])){
                    $nombres[$i]=$_POST["nombre".$i];

                    $i++;
                }else
                    $salir=true;
            }
        $salirNum=false;
        $j=0;
            while (!$salirNum){
                if (isset($_POST["numero".$j])){
                    $numeros[$j]=$_POST["numero".$j];
                    $j++;
                }else
                    $salirNum=true;
            }

In the while, enter, but in the name if, no, and I understand, that is because data does not arrive. I do not know if I explain myself well, I do not want the code solution, I want to know how to treat the hidden and send them. The truth, that php is looking me messy, I come from Java, and I get to an intermediate level in java, I can handle Java vectors, arrayList, etc perfectly, but I'm lost.

I feel for the billet of thread that I have written, and many thanks for your answers. Greetings.

    
asked by cupax64 22.09.2018 в 21:23
source

0 answers