Is there any way to catch the value / content of these input via PHP?

1

As it were, so I have structured everything: Here is the website with fixed textarea and those that can be generated.

 <script language="JavaScript">
       window.onbeforeunload = confirmExit;
        function confirmExit()
        {
            return "Ha intentado salir de esta pagina. Si ha realizado algun cambio en los campos sin hacer clic en el boton Guardar, los cambios se perderan. Seguro que desea salir de esta pagina? ";
        }
        var table1_inputs=0;
        function myCreateFunction()
        {
            var table = document.getElementById("myTable");
            var row = table.insertRow(2);
            var cell2 = row.insertCell(0);
            var input1= document.createElement("textarea");
                input1.rows="4";
                input1.cols="170";
                table1_inputs++;
                input1.name= 'name1'+table1_inputs;
                input1.id = 'id1'+table1_inputs;
            var campo4 = document.createElement("input");
                campo4.type = "button";
                campo4.value = "Borrar";
                campo4.onclick = function ()
                    {
                        var fila = this.parentNode.parentNode;
                        var tbody = table.getElementsByTagName("tbody")[0];
                        tbody.removeChild(fila);
                    }
            cell2.appendChild(input1);
            cell2.appendChild(campo4);
        }

        function vaciar_campo(input1)
        {
            input1.value = "";
        }
        var table2_inputs=0;
        function myCreateFunction2()
        {
            var table = document.getElementById("myTable2");
            var row = table.insertRow(2);
            var cell2 = row.insertCell(0);
            var input2 = document.createElement("textarea");
                input2.rows="4";
                input2.cols="170";
                table2_inputs++;
                input2.name = 'name2'+table2_inputs;
                input2.id = 'id2'+table2_inputs;
            var campo4 = document.createElement("input");
                campo4.type = "button";
                campo4.value = "Borrar";
                campo4.onclick = function ()
                    {
                        var fila = this.parentNode.parentNode;
                        var tbody = table.getElementsByTagName("tbody")[0];
                        tbody.removeChild(fila);
                    }
            cell2.appendChild(input2);
            cell2.appendChild(campo4);
        }

        function vaciar_campo(input2)
        {
            input2.value = "";
        }
        var table3_inputs=0;
        function myCreateFunction3()
        {
            var table = document.getElementById("myTable3");
            var row = table.insertRow(2);
            var cell2 = row.insertCell(0);
            var input3 = document.createElement("textarea");
                input3.rows="4";
                input3.cols="170";
                table3_inputs++;
                input3.name = 'name3'+table3_inputs;
                input3.id = 'id3'+table3_inputs;
            var campo4 = document.createElement("input");
                campo4.type = "button";
                campo4.value = "Borrar";
                campo4.onclick = function ()
                    {
                        var fila = this.parentNode.parentNode;
                        var tbody = table.getElementsByTagName("tbody")[0];
                        tbody.removeChild(fila);
                    }
            cell2.appendChild(input3);
            cell2.appendChild(campo4);
        }

        function vaciar_campo(input3)
        {
            input.value = "";
        }
        var table4_inputs=0;
        function myCreateFunction4()
        {
            var table = document.getElementById("myTable4");
            var row = table.insertRow(2);
            var cell2 = row.insertCell(0);
            var input4 = document.createElement("textarea");
                input4.rows="4";
                input4.cols="170";
                table4_inputs++;
                input4.name = 'name4'+table4_inputs;
                input4.id = 'id4'+table4_inputs;
            var campo4 = document.createElement("input");
                campo4.type = "button";
                campo4.value = "Borrar";
                campo4.onclick = function ()
                    {
                        var fila = this.parentNode.parentNode;
                        var tbody = table.getElementsByTagName("tbody")[0];
                        tbody.removeChild(fila);
                    }
            cell2.appendChild(input4);
            cell2.appendChild(campo4);
        }

        function vaciar_campo(input4)
        {
            input.value = "";
        }
    </script>
</HEAD>
<BODY>
    <form name="formulario" id="formulario" action="creador.php" method="post" width="30%">
        <table id="tablafecha" border="3" width="100%">
                    <tr>
                        <td>
                            <h1>
                                Fecha y número
                            </h1>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Fecha: <input type="text" name="fecha" id="fecha">
                            <br>
                            Número: <input type="number" name="numero" id="numero">
                        </td>
                    </tr>
                </table>
        <table id="myTable" border="3" width="100%">
                    <tr>
                        <td>
                            <h1>
                                Tesis Doctoral
                            </h1>
                       </td>
                    </tr>
                    <tr>
                        <td>
                            <textarea  name="Tesis" id="Tesis" rows="4" cols="170"></textarea>
                            <button onclick="myCreateFunction()" type="button" name="Tesis2" id="Tesis2">
                                Añadir
                            </button>
                        </td>
                    </tr>
                </table>

                <table id="myTable2" border="3" width="100%">
                    <tr>
                        <td>
                            <h1>
                                Subvenciones
                            </h1>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <textarea  name="Subvenciones" id="Subvenciones" rows="4" cols="170"></textarea>
                            <button onclick="myCreateFunction2()" type="button" name="Subvenciones2" id="Subvenciones2">
                                Añadir
                            </button>
                        </td>
                    </tr>
                </table>

                <table id="myTable3" border="3" width="100%">
                    <tr>
                        <td>
                            <h1>
                                Otra informacion
                            </h1>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <textarea  name="Otrainfo" id="Otrainfo" rows="4" cols="170"></textarea>
                            <button onclick="myCreateFunction3()" type="button" name="Otrainfo2" id="Otrainfo2">
                                Añadir
                            </button>
                        </td>
                    </tr>
                </table>

                <table id="myTable4" border="3" width="100%">
                    <tr>
                        <td>
                            <h1>
                                Actualidad
                            </h1>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <textarea  name="Actualidad" id="Actualidad" rows="4" cols="170"></textarea>
                            <button onclick="myCreateFunction4()" type="button" name="actualidad2" id="actualidad2">
                                Añadir
                            </button>
                        </td>
                    </tr>
                </table>
        <br>
        <br>
        <br>
        <center>
            <input type="submit" value="Enviar">
        </center>
    </form>

This is the code of the page that contains the content of the textarea (according to what you have explained to me).

<?php var_dump( $_POST );
  $post = [];
   if( !empty( $_POST ) ) {
  $post = $_POST;
 }
 ?>

 var datos ="<?php foreach( $post as $key => $value ): ?><?php echo $value; ?><?php endforeach; ?>";
 var datos2 ="<?php echo $_POST['Subvenciones']; ?>";
 var datos3 ="<?php echo $_POST['Otrainfo']; ?>";
 var datos4 ="<?php echo $_POST['Actualidad']; ?>";
 var fecha ="<?php echo $_POST['fecha']; ?>";
 var numero ="<?php echo $_POST['numero']; ?>";
 var ventanaHTML = window.open("about:blank", "_blank");

 ventanaHTML.document.writeln(Aquí va una página web la cual posee estilos y le inserto en sus correspondientes lugares, donde quiero que aparezca el contenido que he recogido)

The result within the web page would be:

Doctoral thesis

  • Information written in the fixed textarea called Thesis
  • Information written in the textarea generated from the button (name11)
  • Information written in the textarea generated by the button (name12)
  • Grants

  • Information written in the fixed textarea called Subsidies
  • Information written in the textarea generated from the button (name21)
  • Information written in the textarea generated from the button (name22)
  • What problems occur to me? The information I collect is repeated in both Thesis and Grants. More li than they should be created, taking into account that what is generated by javascript.

    EDITED 3

    And if for example you want the created array not to be combined with the first of each section, how would you do it? For example, the field already created is thesis but the array I want it to be with the id, name otesis. I've been testing and it's not coming out.

        
    asked by Oscar Sanclemente 22.03.2017 в 09:55
    source

    1 answer

    1

    With loose information we got together.

    This is a quick adaptation of what you have to see the data sent by the form and present them in the browser. It is a base to work on and further develop your idea.

    The first part obtains the form data and uses var_dump to give "visual" output in the browser to the data that is received.

    Next, check if $_POST contains values and assign them to a variable to be treated later with foreach , the iterator that commented.

    As a result of the question and the comments, I believe that it gives an answer, or at least it is shown with more clarity than there is behind a submission of a form and the key / value data that you need to continue developing.

    I hope it helps and anything about it, comment and we continue.

    File name - 57053.php

    <?php
    /**
     * Permite ver los datos enviados por el formulario
     * para observar posibles errores en el desarrollo
     */
    var_dump( $_POST );
    $post = [];
    // si $_POST tiene valores se le asigna a una variable
    // para tratarla en el foreach
    if( !empty( $_POST ) ) {
        $post = $_POST;
    }
    ?>
    <!DOCTYPE html>
    <html lang="es">
    <head>
        <meta charset="UTF-8">
        <title>57053</title>
    </head>
    <body>
    
    <!-- cambiar el action por el destino de procesamiento -->
    <form action="57053.php" method="POST">
        <table id="myTable" border="3" width="100%">
            <tr>
                <td>
                    <h1>Tesis Doctoral</h1>
                </td>
            </tr>
            <tr>
                <td>
                    <textarea name="Tesis" id="Tesis" rows="4" cols="170"></textarea>
                </td>
            </tr>
        </table>
        <!-- crear nuevos campos textarea -->
        <button onclick="myCreateFunction()" type="button" name="Tesis2" id="Tesis2">Añadir</button>
        <!-- envia el formulario -->
        <input type="submit" value="Enviar">
    </form>
    
    <table border="3" width="100%">
        <tr>
            <td>Nombre textarea</td><td>Valor textarea</td>
        </tr>
        <!-- iteramos el contenido -->
        <?php foreach( $post as $key => $value ): ?>
        <tr>
            <td><?php echo $key; ?></td><td><?php echo $value; ?></td>
        </tr>
        <?php endforeach; ?>
    </table>
    
    <script type="text/javascript">
    var table1_inputs = 0;
    function myCreateFunction() {
        var table      = document.getElementById("myTable");
        var row        = table.insertRow(2);
        var cell2      = row.insertCell(0);
        var input1     = document.createElement("textarea");
        input1.rows    = "4";
        input1.cols    = "170";
        table1_inputs++;
        input1.name    = 'name1' + table1_inputs;
        input1.id      = 'id1' + table1_inputs;
        var campo4     = document.createElement("input");
        campo4.type    = "button";
        campo4.value   = "Borrar";
        campo4.onclick = function () {
            var fila  = this.parentNode.parentNode;
            var tbody = table.getElementsByTagName("tbody")[0];
            tbody.removeChild(fila);
        }
        cell2.appendChild(input1);
        cell2.appendChild(campo4);
    }
    </script>
    </body>
    </html>
    

    About the comment to use the <li> tags. It can perfectly. Changing what surrounds the variables you need.

    <?php foreach( $post as $key => $value ): ?>
        <li><?php echo $value; ?></li>
    <?php endforeach; ?>
    

    To take into account that of the repetition, if you assign it to a variable javascript without any format or structure it will appear as a continuous text. Like the <li> you should give the structure you want to get the desired result.

    Edited 2

    With regard to the order, you can separate them into categories since you have them well defined. Thesis, subsidies, other information ...

    Likewise, it is about isolating each category in a different array .

    To start, you have to make some adjustments to the names and ids of the tables and textarea . As well as in the javascript function that is responsible for creating new elements. The modification is quite basic to not have duplicate functions.

    Once you have the categories separated into different variables, you can manage them as you wish.

    <?php
    /**
     * Permite ver los datos enviados por el formulario
     * para observar posibles errores en el desarrollo
     */
    //var_dump( $_POST );
    $post = [];
    // si $_POST tiene valores se le asigna a una variable
    // para tratarla en el foreach
    if( !empty( $_POST ) ) {
        $post = $_POST;
    }
    // separa el array por las key de la misma categoría
    $tesis        = array_intersect_key( $post, array_flip( preg_grep( '/^tesis[0-9]*/i', array_keys( $post ) ) ) );
    $subvenciones = array_intersect_key( $post, array_flip( preg_grep( '/^subvenciones[0-9]*/i', array_keys( $post ) ) ) );
    $otrainfo     = array_intersect_key( $post, array_flip( preg_grep( '/^otrainfo[0-9]*/i', array_keys( $post ) ) ) );
    $actualidad   = array_intersect_key( $post, array_flip( preg_grep( '/^actualidad[0-9]*/i', array_keys( $post ) ) ) );
    //var_dump( $post['fecha'], $post['numero'], $tesis, $subvenciones, $otrainfo, $actualidad );
    ?>
    <!DOCTYPE html>
        <html lang="es">
        <head>
            <meta charset="UTF-8">
            <title>57053</title>
        </head>
        <body>
    
        <form name="formulario" id="formulario" action="57053-2.php" method="post" width="30%">
            <table id="tablafecha" border="3" width="100%">
                <tr>
                    <td><h1>Fecha y número</h1>
                    </td>
                </tr>
                <tr>
                    <td>Fecha: <input type="text" name="fecha" id="fecha"><br>
                        Número: <input type="number" name="numero" id="numero">
                    </td>
                </tr>
            </table>
    
            <table id="tesisTable" border="3" width="100%">
                <tr>
                    <td><h1>Tesis Doctoral</h1>
                   </td>
                </tr>
                <tr>
                    <td>
                        <textarea name="tesis" id="tesis" rows="4" cols="170"></textarea>
                        <button onclick="addTextarea('tesisTable', 'tesis')" type="button" name="tesis2" id="tesis2">Añadir</button>
                    </td>
                </tr>
            </table>
    
            <table id="subvencionesTable" border="3" width="100%">
                <tr>
                    <td><h1>Subvenciones</h1></td>
                </tr>
                <tr>
                    <td>
                        <textarea name="subvenciones" id="subvenciones" rows="4" cols="170"></textarea>
                        <button onclick="addTextarea('subvencionesTable', 'subvenciones')" type="button" name="subvenciones2" id="subvenciones2">Añadir</button>
                    </td>
                </tr>
            </table>
    
            <table id="otrainfoTable" border="3" width="100%">
                <tr>
                    <td><h1>Otra informacion</h1>
                    </td>
                </tr>
                <tr>
                    <td>
                        <textarea name="otrainfo" id="otrainfo" rows="4" cols="170"></textarea>
                        <button onclick="addTextarea('otrainfoTable', 'otrainfo')" type="button" name="otrainfo2" id="otrainfo2">Añadir</button>
                    </td>
                </tr>
            </table>
    
            <table id="actualidadTable" border="3" width="100%">
                <tr>
                    <td><h1>Actualidad</h1></td>
                </tr>
                <tr>
                    <td>
                        <textarea name="actualidad" id="actualidad" rows="4" cols="170"></textarea>
                        <button onclick="addTextarea('actualidadTable', 'actualidad')" type="button" name="actualidad2" id="actualidad2">Añadir</button>
                    </td>
                </tr>
            </table>
            <br>
            <br>
            <br>
            <center>
                <input type="submit" value="Enviar">
            </center>
        </form>
    
        <!-- iteramos el contenido -->
        <div>
            <h5>Tesis doctoral</h5>
            <ul>
                <?php foreach( $tesis as $key => $value ): ?>
                    <li><?php echo $value; ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
        <div>
            <h5>Suvbenciones</h5>
            <ul>
                <?php foreach( $subvenciones as $key => $value ): ?>
                    <li><?php echo $value; ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
        <div>
            <h5>Otra información</h5>
            <ul>
                <?php foreach( $otrainfo as $key => $value ): ?>
                    <li><?php echo $value; ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
        <div>
            <h5>Actualidad</h5>
            <ul>
                <?php foreach( $actualidad as $key => $value ): ?>
                    <li><?php echo $value; ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    
        <script language="JavaScript">
            window.onbeforeunload = confirmExit;
            function confirmExit() {
                return "Ha intentado salir de esta pagina. Si ha realizado algun cambio en los campos sin hacer clic en el boton Guardar, los cambios se perderan. Seguro que desea salir de esta pagina? ";
            }
    
            var table_inputs = 0;
            function addTextarea(id, name) {
                var table     = document.getElementById(id);
                var row       = table.insertRow(2);
                var cell      = row.insertCell(0);
                var input     = document.createElement("textarea");
                table_inputs++;
                input.rows    = "4";
                input.cols    = "170";
                input.name    = name + table_inputs;
                input.id      = id + table_inputs;
                var campo     = document.createElement("input");
                campo.type    = "button";
                campo.value   = "Borrar";
                campo.onclick = function () {
                    var fila  = this.parentNode.parentNode;
                    var tbody = table.getElementsByTagName("tbody")[0];
                    tbody.removeChild(fila);
                }
                cell.appendChild(input);
                cell.appendChild(campo);
            }
    
            function vaciar_campo(input1) {
                input1.value = "";
            }
        </script>
    </body>
    </html>
    
        
    answered by 22.03.2017 / 11:05
    source