Visualizzazione dei risultati da 1 a 2 su 2

Discussione: Form Dinamico

  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2006
    Messaggi
    11

    Form Dinamico

    Ciao a tutti,
    devo creare un form tipo questo

    in pratica name rappresenta il nome di una funzione e quando l'utente preme add, deve apparire un secondo attributo (come in figura). Quando l'utente preme il secondo add, appare un terzo attributo e così via.

    Avreste qualche esempio simile o qualche consiglio?

    Il problema è anche legato al fatto che quando l'utente schiaccia submit devono essere inviati tutti gli attributi, quindi bisognerà gestire in qualche modo i nomi con un contatore tipo att1, att2...

  2. #2
    Ti copio/incollo del codice che ho scritto tempo fa e con qualche piccola modifica puoi adattarlo alle tue esigenze dato che fa grossomodo ciò di cui hai bisogno. Per approfondire puoi dare un'occhiata al DOM

    Codice PHP:
    <?php
    if (isset($_POST['submit'])) {
        for (
    $i 0$i count($_POST['name']); $i++) {
            echo 
    $_POST['name'][$i]." ".$_POST['surname'][$i]."
    "
    ;
        }
    }
    ?>

    <html>
        <head>
            <script type="text/javascript">
                function add() {
                    var tr = document.createElement("tr");
                    var td1 = document.createElement("td");
                    var td2 = document.createElement("td");
                    var td3 = document.createElement("td");
                    var td4 = document.createElement("td");
                    var name = document.createTextNode("Nome:");
                    var surname = document.createTextNode("Cognome:");
                    var nameInput = document.createElement("input");
                    var surnameInput = document.createElement("input");

                  nameInput.setAttribute("type", "text");
                    nameInput.setAttribute("name", "name[]");
                  surnameInput.setAttribute("type", "text");
                    surnameInput.setAttribute("name", "surname[]");

                    td1.appendChild(name);
                    td2.appendChild(nameInput);
                    td3.appendChild(surname);
                    td4.appendChild(surnameInput);
                    tr.appendChild(td1);
                    tr.appendChild(td2);
                    tr.appendChild(td3);
                    tr.appendChild(td4);
                    document.getElementById('tabella').getElementsByTagName('tbody')[0].appendChild(tr);
                }
            </script>
        </head>
        <body>
            <form method="post" action="index.php">
                <table id="tabella">
                    <tbody>
                        <tr>
                            <td><input type="button" value="ADD" onclick="add()"/></td>
                            <td><input type="submit" name="submit" value="OK"/></td>
                        </tr>
                    </tbody>
                </table>
            </form>
        </body>
    </html>
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.