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

    passaggio di parametri contenuto td

    salve a tutti, ho bisogno di aiuto.
    Ho un form method post, all'interno del quale c'è una <table>...io vorrei recuperare i dati contenuti nei vari <td> della table e passarli allo script php riferito dal form.

    Ma facendo da php $_POST['id_del_td']; mi ritorna undefined index.

    Come faccio a passare via post il contenuto dei td? (premetto che il contenuto lo posso recuperare col dom javascript ma vorrei passarlo al php)

    grazie a tutti

  2. #2
    no, allora, per passare dei parametri tramite una richiesta POST a partire da una form, devi usare gli input (ed i select ed i textarea) cioè gli elementi studiati per l'inserimento di dati da parte dell'utente, dati che vengono poi inviati alle pagine dinamiche (PHP nel tuo caso) e letti come hai scritto tu (anche se devi usare il nome dell'elemento e non l'id).

    Secondo me, fai prima a modificare la procedura che genera la table, per introdurre nei diversi TD anche un input ti tipo hidden (quindi non visualizzato) con il valore corrispondente.

    Ad esempio, devi ottenere alla fine:

    Codice PHP:
    <form action="..." method "POST">
       <
    table>
          <
    tr>
             <
    td>Valore 1<input type="hidden" name="tabella[0][0]" value ="Valore 1" /></td>
             <
    td>Valore 2<input type="hidden" name="tabella[0][1]" value ="Valore 2" /></td>
             <
    td>Valore 3<input type="hidden" name="tabella[0][2]" value ="Valore 3" /></td>
             <
    td>Valore 4<input type="hidden" name="tabella[0][3]" value ="Valore 4" /></td>
          </
    tr>
          <
    tr>
             <
    td>Valore 5<input type="hidden" name="tabella[1][0]" value ="Valore 5" /></td>
             <
    td>Valore 6<input type="hidden" name="tabella[1][1]" value ="Valore 6" /></td>
             <
    td>Valore 7<input type="hidden" name="tabella[1][2]" value ="Valore 7" /></td>
             <
    td>Valore 8<input type="hidden" name="tabella[1][3]" value ="Valore 8" /></td>
          </
    tr>
          <
    tr>
             <
    td>Valore 9<input type="hidden" name="tabella[2][0]" value ="Valore 9" /></td>
             <
    td>Valore 10<input type="hidden" name="tabella[2][1]" value ="Valore 10" /></td>
             <
    td>Valore 11<input type="hidden" name="tabella[2][2]" value ="Valore 11" /></td>
             <
    td>Valore 12<input type="hidden" name="tabella[2][3]" value ="Valore 12" /></td>
          </
    tr>
       </
    table>
    </
    form
    Quando invierai la form dal PHP otterrai i dati:

    Codice PHP:
    $dati $_POST["tabella"];

    echo 
    $dati[0][2]; //Stampi:  Valore 3
    echo $dati[2][0]; //Stampi:  Valore 9 
    e così via.

    Se invece non hai modo di modificare il contenuto dell'HTML, lo puoi fare in JS, ma secondo me è molto più semplice nel medoto sopracitato.

    Comunque, l'idea è la stessa: metti di avere la tabella ed un sistema per identificare le celle

    Codice PHP:
    <form id="MyForm" action "..." method "POST">
        <
    table id "MyTable">
          <
    tr>
             <
    td>Valore da prendere</td>
             <
    td>Valore da prendere 2</td>
             <
    td>Valore da prendere 3</td>
             <
    td>Valore da prendere Reloaded</td>
             <
    td>Valore da prendere Ultimate</td>
          </
    tr>
          <
    tr>
             <
    td>Valore da prendere Last chance</td>
             <
    td>Valore da prendere hohoho</td>
             <
    td>Valore da prendere ...</td>
             <
    td>Valore da prendere mi sono stufato</td>
             <
    td>Valore da prendere</td>
          </
    tr>
          <
    tr>
             <
    td>Valore da prendere</td>
             <
    td>Valore da prendere</td>
             <
    td>Valore da prendere</td>
             <
    td>Valore da prendere</td>
             <
    td>Valore da prendere</td>
          </
    tr>
        </
    table>
    </
    form
    a questo punto fai una funzione che esamini la tabella ed inserisca gli input hidden nella form.

    Codice PHP:
    //Viene dato questo nome agli input
    var generalName "tabella";

    var 
    CreateInput = function(namevalue){
        var 
    tmp document.createElement("input");
        
    tmp.value value;
        
    tmp.name name;
        
    tmp.type "HIDDEN";
        return 
    tmp;
    }

    //Questo accetta come argomento il tbody della tabella
    var WalkThroughTable = function(tbody){
        var 
    child;
        var 
    index 0;
        var 
    inputs = [];
        for(
    child tbody.firstChildchild != nullchild child.nextSibling){
            if(
    child.nodeType == 1){
               if(
    child.tagName.toUpperCase() == "TR"){ //È un TR
                   
    inputs inputs.concat(WalkThroughRow(childindex++));
               }
            }
        }
        return 
    inputs;
    }

    var 
    WalkThroughRow = function(rowindexRow){
        var 
    child;
        var 
    inputs = []; //Array per il ritorno
        
    var index 0;
        for(
    child row.firstChildchild != nullchild child.nextSibling){
           if(
    child.nodeType == 1){ //È un Element
               
    if(child.tagName.toUpperCase() == "TD"){ //È un TD
                   
    inputs.push(Td2Input(childindexRowindex++));
               }
           }
        }

        return 
    inputs;
    }

    var 
    Td2Input = function(tdindexRowindexCell){
        var 
    value td.innerHTML;
        var 
    name generalName+"["+indexRow+"]["+indexCell+"]";
        return 
    CreateInput(namevalue);
    }


    //Chiamata Generale
    var ExtractValueFromCell = function(tableIDformElem){
        if(!
    tableID) throw "l'id della tabella non può essere vuoto";
        var 
    table document.getElementById(tableID);
        var 
    tbody null;
        for(
    tbody table.firstChildtbody != nulltbody tbody.nextSibling){
            if(
    tbody.nodeType == 1){//È un elemento
               
    if(tbody.tagName.toUpperCase() == "TBODY") break;
            }
        }

        var 
    inputs WalkThroughTable(tbody);
        for(var 
    0inputs.lengthi++){
           var 
    inpt inputs[i];
           
    formElem.appendChild(inpt);
        }
        return 
    true;
    }

    //Questa chiamata la metto come evento onSubmit della form
    window.onload = function(){
       
    document.getElementById("MyForm").onsubmit = function(){
          
    ExtractValueFromCell("MyTable"this);
       };
    }; 
    Mi sembra vada, ma non l'ho provato molto
    I DON'T Double Click!

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.