Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    Campi di un form Clonati non riconosciuti...

    Ciao, volevo fare un form in cui si permetteva agli utenti di aggiungere eventuali campi opzionali in pių "clonando" quello definito in HTML.

    Per ottenere questo ho scritto il seguente codice (qui ce n'č solo un'estrapolazione).
    Codice PHP:
    <?php

    if (isset($_POST) && !empty($_POST))
        
    print_r($_POST);

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
        <head>
            <title>Testtttt</title>
            
            <script type="text/javascript">
                var count = 0;
                
                function updateId(element, oldID, newID) {
                    element.id.replace("["+(oldID-1)+"]", "["+(count)+"]")
                }
                
                function addAnotherField() {
                    var upNew = document.getElementById("Input["+count+"]").cloneNode(true);
                    count++;

                    upNew.id = upNew.id.replace("["+(count-1)+"]", "["+(count)+"]");

                    var fields = upNew.getElementsByTagName("input");

                    for (var i = 0; i < fields.length; i++) {
                        fields[i].id = fields[i].id.replace("["+(count-1)+"]",
                                                     "["+(count)+"]");
                        fields[i].name = fields[i].name.replace("["+(count-1)+"]",
                                                        "["+(count)+"]");

                        if (fields[i].id == "Data["+count+"]")
                            fields[i].value = "no";

                        if (fields[i].id == "Info["+count+"]")
                            fields[i].value = "ok";
                    }

                    document.getElementById("Inputs").appendChild(upNew);
                };
                
            </script>
        </head>

        <body>
            <table>
                <form method="post">
                    <tbody id="Inputs">
                        <tr id="Input[0]">
                            <td>
                                <input id="Data[0]" type="text" name="nice[0]" value="no" style="width:200px;" onclick="if (this.value == 'no') this.value='';" onblur="if (this.value == '') this.value='no';" />
                            </td>
                            <td>
                                <input id="Info[0]" type="text" value="ok" name="bad[0]" style="width:200px;" onclick="if (this.value == 'ok') this.value='';" onblur="if (this.value == '') this.value='ok';" />
                            </td>
                        </tr>
                    </tbody>
                    <tr>
                        <td>
                            <input type="button" value="+" onclick="addAnotherField();" />
                        </td>
                        <td>
                            <input type="submit" name="send" value="Ok" />
                        </td>
                    </tr>
                </form>
            </table>
        </body>
    </html>
    Il problema č che, come potete provare in da questa pagina, una volta creati i nuovi campi ed inserito il contenuto, se si preme il pulsante d'invio, PHP non riceve niente se non il primo campo inserito.

    Praticamente, tutti i campi Data[i+1] e Info[i+1] correttamente generati (ho controllato con i vari debugger per FF) non vengono inclusi nel form...

    La cosa strana č che se io facessi qualcosa tipo:
    codice:
    var fields = document.getElementsByTagName("input");
    for (var i = 0; i < fields.length; i++) {
    	fields[i].disabled = "true";
    }
    Tutti i campi verrebbero disabilitati; compresi quelli che ho creato io tramite la "clonazione"... Quindi di fatto vengono inseriti correttamente nell'albero DOM.

    Il vero problema, mi pare che sia nel form; che di fatto non riconosce come "propri" gli elementi aggiunti.

    Qualche idea? Grazie...
    www.ClonyItaly.tk - ClonyXXL Italian Site

    Powered by tin.it ADSL 4096/256 Senza fonia! - Leggi la mia esperienza di attivazione
    VoIP by Skypho + LinkSys PAP2 Unlocked.

  2. #2
    Prova a fare:

    codice:
        <body> 
             <form method="post" action=""> 
               <table> 
                    <tbody id="Inputs"> 
                        <tr id="Input[0]"> 
                            <td> 
                                <input id="Data[0]" type="text" name="nice[0]" value="no" style="width:200px;" onclick="if (this.value == 'no') this.value='';" onblur="if (this.value == '') this.value='no';" /> 
                            </td> 
                            <td> 
                                <input id="Info[0]" type="text" value="ok" name="bad[0]" style="width:200px;" onclick="if (this.value == 'ok') this.value='';" onblur="if (this.value == '') this.value='ok';" /> 
                            </td> 
                        </tr> 
                    </tbody> 
                    <tr> 
                        <td> 
                            <input type="button" value="+" onclick="addAnotherField();" /> 
                        </td> 
                        <td> 
                            <input type="submit" name="send" value="Ok" /> 
                        </td> 
                    </tr> 
    
               </table> 
            </form> 
        </body>
    I DON'T Double Click!

  3. #3
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    infatti, il form e' in posizione errata

  4. #4
    Grazie invertendo la posizione di form e table funziona...
    www.ClonyItaly.tk - ClonyXXL Italian Site

    Powered by tin.it ADSL 4096/256 Senza fonia! - Leggi la mia esperienza di attivazione
    VoIP by Skypho + LinkSys PAP2 Unlocked.

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.